<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/16">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Redis/24. redis 配置/">redis 配置</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis 的配置文件位于 Redis 安装目录下，文件名为 redis.conf。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以通过 <strong>CONFIG</strong> 命令查看或设置配置项。</p>
<h2 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h2><h3 id="Redis-CONFIG-命令格式如下："><a href="#Redis-CONFIG-命令格式如下：" class="headerlink" title="Redis CONFIG 命令格式如下："></a>Redis CONFIG 命令格式如下：</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; CONFIG GET CONFIG_SETTING_NAME</div></pre></td></tr></table></figure>
<h3 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; CONFIG GET loglevel</div><div class="line"> </div><div class="line">1) <span class="string">"loglevel"</span></div><div class="line">2) <span class="string">"notice"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 * 号获取所有配置项：</p>
<h3 id="实例-1"><a href="#实例-1" class="headerlink" title="实例"></a>实例</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; CONFIG GET *</div><div class="line"></div><div class="line">1) <span class="string">"dbfilename"</span></div><div class="line">2) <span class="string">"dump.rdb"</span></div><div class="line">3) <span class="string">"requirepass"</span></div><div class="line">4) <span class="string">""</span></div><div class="line">5) <span class="string">"masterauth"</span></div><div class="line">6) <span class="string">""</span></div><div class="line">7) <span class="string">"unixsocket"</span>          </div><div class="line">8) <span class="string">""</span></div><div class="line">9) <span class="string">"logfile"</span></div><div class="line">10) <span class="string">""</span></div><div class="line">11) <span class="string">"pidfile"</span></div><div class="line">12) <span class="string">"/var/run/redis.pid"</span></div><div class="line">13) <span class="string">"maxmemory"</span></div><div class="line">14) <span class="string">"0"</span></div><div class="line">15) <span class="string">"maxmemory-samples"</span></div><div class="line">16) <span class="string">"3"</span></div><div class="line">17) <span class="string">"timeout"</span></div><div class="line">18) <span class="string">"0"</span></div><div class="line">19) <span class="string">"tcp-keepalive"</span></div><div class="line">20) <span class="string">"0"</span></div><div class="line">21) <span class="string">"auto-aof-rewrite-percentage"</span></div><div class="line">22) <span class="string">"100"</span></div><div class="line">23) <span class="string">"auto-aof-rewrite-min-size"</span></div><div class="line">24) <span class="string">"67108864"</span></div><div class="line">25) <span class="string">"hash-max-ziplist-entries"</span></div><div class="line">26) <span class="string">"512"</span></div><div class="line">27) <span class="string">"hash-max-ziplist-value"</span></div><div class="line">28) <span class="string">"64"</span></div><div class="line">29) <span class="string">"list-max-ziplist-entries"</span></div><div class="line">30) <span class="string">"512"</span></div><div class="line">31) <span class="string">"list-max-ziplist-value"</span></div><div class="line">32) <span class="string">"64"</span></div><div class="line">33) <span class="string">"set-max-intset-entries"</span></div><div class="line">34) <span class="string">"512"</span></div><div class="line">35) <span class="string">"zset-max-ziplist-entries"</span></div><div class="line">36) <span class="string">"128"</span></div><div class="line">37) <span class="string">"zset-max-ziplist-value"</span></div><div class="line">38) <span class="string">"64"</span></div><div class="line">39) <span class="string">"hll-sparse-max-bytes"</span></div><div class="line">40) <span class="string">"3000"</span></div><div class="line">41) <span class="string">"lua-time-limit"</span></div><div class="line">42) <span class="string">"5000"</span></div><div class="line">43) <span class="string">"slowlog-log-slower-than"</span></div><div class="line">44) <span class="string">"10000"</span></div><div class="line">45) <span class="string">"latency-monitor-threshold"</span></div><div class="line">46) <span class="string">"0"</span></div><div class="line">47) <span class="string">"slowlog-max-len"</span></div><div class="line">48) <span class="string">"128"</span></div><div class="line">49) <span class="string">"port"</span></div><div class="line">50) <span class="string">"6379"</span></div><div class="line">51) <span class="string">"tcp-backlog"</span></div><div class="line">52) <span class="string">"511"</span></div><div class="line">53) <span class="string">"databases"</span></div><div class="line">54) <span class="string">"16"</span></div><div class="line">55) <span class="string">"repl-ping-slave-period"</span></div><div class="line">56) <span class="string">"10"</span></div><div class="line">57) <span class="string">"repl-timeout"</span></div><div class="line">58) <span class="string">"60"</span></div><div class="line">59) <span class="string">"repl-backlog-size"</span></div><div class="line">60) <span class="string">"1048576"</span></div><div class="line">61) <span class="string">"repl-backlog-ttl"</span></div><div class="line">62) <span class="string">"3600"</span></div><div class="line">63) <span class="string">"maxclients"</span></div><div class="line">64) <span class="string">"4064"</span></div><div class="line">65) <span class="string">"watchdog-period"</span></div><div class="line">66) <span class="string">"0"</span></div><div class="line">67) <span class="string">"slave-priority"</span></div><div class="line">68) <span class="string">"100"</span></div><div class="line">69) <span class="string">"min-slaves-to-write"</span></div><div class="line">70) <span class="string">"0"</span></div><div class="line">71) <span class="string">"min-slaves-max-lag"</span></div><div class="line">72) <span class="string">"10"</span></div><div class="line">73) <span class="string">"hz"</span></div><div class="line">74) <span class="string">"10"</span></div><div class="line">75) <span class="string">"no-appendfsync-on-rewrite"</span></div><div class="line">76) <span class="string">"no"</span></div><div class="line">77) <span class="string">"slave-serve-stale-data"</span></div><div class="line">78) <span class="string">"yes"</span></div><div class="line">79) <span class="string">"slave-read-only"</span></div><div class="line">80) <span class="string">"yes"</span></div><div class="line">81) <span class="string">"stop-writes-on-bgsave-error"</span></div><div class="line">82) <span class="string">"yes"</span></div><div class="line">83) <span class="string">"daemonize"</span></div><div class="line">84) <span class="string">"no"</span></div><div class="line">85) <span class="string">"rdbcompression"</span></div><div class="line">86) <span class="string">"yes"</span></div><div class="line">87) <span class="string">"rdbchecksum"</span></div><div class="line">88) <span class="string">"yes"</span></div><div class="line">89) <span class="string">"activerehashing"</span></div><div class="line">90) <span class="string">"yes"</span></div><div class="line">91) <span class="string">"repl-disable-tcp-nodelay"</span></div><div class="line">92) <span class="string">"no"</span></div><div class="line">93) <span class="string">"aof-rewrite-incremental-fsync"</span></div><div class="line">94) <span class="string">"yes"</span></div><div class="line">95) <span class="string">"appendonly"</span></div><div class="line">96) <span class="string">"no"</span></div><div class="line">97) <span class="string">"dir"</span></div><div class="line">98) <span class="string">"/home/deepak/Downloads/redis-2.8.13/src"</span></div><div class="line">99) <span class="string">"maxmemory-policy"</span></div><div class="line">100) <span class="string">"volatile-lru"</span></div><div class="line">101) <span class="string">"appendfsync"</span></div><div class="line">102) <span class="string">"everysec"</span></div><div class="line">103) <span class="string">"save"</span></div><div class="line">104) <span class="string">"3600 1 300 100 60 10000"</span></div><div class="line">105) <span class="string">"loglevel"</span></div><div class="line">106) <span class="string">"notice"</span></div><div class="line">107) <span class="string">"client-output-buffer-limit"</span></div><div class="line">108) <span class="string">"normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"</span></div><div class="line">109) <span class="string">"unixsocketperm"</span></div><div class="line">110) <span class="string">"0"</span></div><div class="line">111) <span class="string">"slaveof"</span></div><div class="line">112) <span class="string">""</span></div><div class="line">113) <span class="string">"notify-keyspace-events"</span></div><div class="line">114) <span class="string">""</span></div><div class="line">115) <span class="string">"bind"</span></div><div class="line">116) <span class="string">""</span></div></pre></td></tr></table></figure>
<h2 id="编辑配置"><a href="#编辑配置" class="headerlink" title="编辑配置"></a>编辑配置</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以通过修改 redis.conf 文件或使用 <strong>CONFIG set</strong> 命令来修改配置。</p>
<h3 id="语法-1"><a href="#语法-1" class="headerlink" title="语法"></a>语法</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>CONFIG SET</strong> 命令基本语法：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE</div></pre></td></tr></table></figure>
<h3 id="实例-2"><a href="#实例-2" class="headerlink" title="实例"></a>实例</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; CONFIG SET loglevel <span class="string">"notice"</span></div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; CONFIG GET loglevel</div><div class="line"> </div><div class="line">1) <span class="string">"loglevel"</span></div><div class="line">2) <span class="string">"notice"</span></div></pre></td></tr></table></figure>
<h2 id="参数说明"><a href="#参数说明" class="headerlink" title="参数说明"></a>参数说明</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis.conf 配置项说明如下：</p>
<ol>
<li>Redis默认不是以守护进程的方式运行，可以通过该配置项修改，使用yes启用守护进程</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">daemonize no</div></pre></td></tr></table></figure>
<ol>
<li>当Redis以守护进程方式运行时，Redis默认会把pid写入/var/run/redis.pid文件，可以通过pidfile指定</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pidfile /var/run/redis.pid</div></pre></td></tr></table></figure>
<ol>
<li>指定Redis监听端口，默认端口为6379，作者在自己的一篇博文中解释了为什么选用6379作为默认端口，因为6379在手机按键上MERZ对应的号码，而MERZ取自意大利歌女Alessia Merz的名字</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">port 6379</div></pre></td></tr></table></figure>
<ol>
<li>绑定的主机地址</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">bind</span> 127.0.0.1</div></pre></td></tr></table></figure>
<p>5.当 客户端闲置多长时间后关闭连接，如果指定为0，表示关闭该功能</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">timeout 300</div></pre></td></tr></table></figure>
<ol>
<li>指定日志记录级别，Redis总共支持四个级别：debug、verbose、notice、warning，默认为verbose</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">loglevel verbose</div></pre></td></tr></table></figure>
<ol>
<li>日志记录方式，默认为标准输出，如果配置Redis为守护进程方式运行，而这里又配置为日志记录方式为标准输出，则日志将会发送给/dev/null</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">logfile stdout</div></pre></td></tr></table></figure>
<ol>
<li>设置数据库的数量，默认数据库为0，可以使用SELECT <dbid>命令在连接上指定数据库id</dbid></li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">databases 16</div></pre></td></tr></table></figure>
<ol>
<li>指定在多长时间内，有多少次更新操作，就将数据同步到数据文件，可以多个条件配合</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">save &lt;seconds&gt; &lt;changes&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis默认配置文件中提供了三个条件：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">save 900 1</div><div class="line">save 300 10</div><div class="line">save 60 10000</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;分别表示900秒（15分钟）内有1个更改，300秒（5分钟）内有10个更改以及60秒内有10000个更改。</p>
<ol>
<li>指定存储至本地数据库时是否压缩数据，默认为yes，Redis采用LZF压缩，如果为了节省CPU时间，可以关闭该选项，但会导致数据库文件变的巨大</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rdbcompression yes</div></pre></td></tr></table></figure>
<ol>
<li>指定本地数据库文件名，默认值为dump.rdb</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">dbfilename dump.rdb</div></pre></td></tr></table></figure>
<ol>
<li>指定本地数据库存放目录</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">dir ./</div></pre></td></tr></table></figure>
<ol>
<li>设置当本机为slav服务时，设置master服务的IP地址及端口，在Redis启动时，它会自动从master进行数据同步</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">slaveof &lt;masterip&gt; &lt;masterport&gt;</div></pre></td></tr></table></figure>
<ol>
<li>当master服务设置了密码保护时，slav服务连接master的密码</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">masterauth &lt;master-password&gt;</div></pre></td></tr></table></figure>
<ol>
<li>设置Redis连接密码，如果配置了连接密码，客户端在连接Redis时需要通过AUTH <password>命令提供密码，默认关闭</password></li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">requirepass foobared</div></pre></td></tr></table></figure>
<ol>
<li>设置同一时间最大客户端连接数，默认无限制，Redis可以同时打开的客户端连接数为Redis进程可以打开的最大文件描述符数，如果设置 maxclients 0，表示不作限制。当客户端连接数到达限制时，Redis会关闭新的连接并向客户端返回max number of clients reached错误信息</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">maxclients 128</div></pre></td></tr></table></figure>
<ol>
<li>指定Redis最大内存限制，Redis在启动时会把数据加载到内存中，达到最大内存后，Redis会先尝试清除已到期或即将到期的Key，当此方法处理 后，仍然到达最大内存设置，将无法再进行写入操作，但仍然可以进行读取操作。Redis新的vm机制，会把Key存放内存，Value会存放在swap区</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">maxmemory &lt;bytes&gt;</div></pre></td></tr></table></figure>
<ol>
<li>指定是否在每次更新操作后进行日志记录，Redis在默认情况下是异步的把数据写入磁盘，如果不开启，可能会在断电时导致一段时间内的数据丢失。因为 redis本身同步数据文件是按上面save条件来同步的，所以有的数据会在一段时间内只存在于内存中。默认为no</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">appendonly no</div></pre></td></tr></table></figure>
<ol>
<li>指定更新日志文件名，默认为appendonly.aof</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">appendfilename appendonly.aof</div></pre></td></tr></table></figure>
<ol>
<li>指定更新日志条件，共有3个可选值： </li>
</ol>
<ul>
<li><strong>no</strong>：表示等操作系统进行数据缓存同步到磁盘（快） </li>
<li><strong>always</strong>：表示每次更新操作后手动调用fsync()将数据写到磁盘（慢，安全） </li>
<li><strong>everysec</strong>：表示每秒同步一次（折衷，默认值）</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">appendfsync everysec</div><div class="line">``` </div><div class="line"></div><div class="line">21. 指定是否启用虚拟内存机制，默认值为no，简单的介绍一下，VM机制将数据分页存放，由Redis将访问量较少的页即冷数据swap到磁盘上，访问多的页面由磁盘自动换出到内存中（在后面的文章我会仔细分析Redis的VM机制）</div><div class="line"></div><div class="line">```bash</div><div class="line">vm-enabled no</div></pre></td></tr></table></figure>
<ol>
<li>虚拟内存文件路径，默认值为/tmp/redis.swap，不可多个Redis实例共享</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vm-swap-file /tmp/redis.swap</div></pre></td></tr></table></figure>
<ol>
<li>将所有大于vm-max-memory的数据存入虚拟内存,无论vm-max-memory设置多小,所有索引数据都是内存存储的(Redis的索引数据 就是keys),也就是说,当vm-max-memory设置为0的时候,其实是所有value都存在于磁盘。默认值为0</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vm-max-memory 0</div></pre></td></tr></table></figure>
<ol>
<li>Redis swap文件分成了很多的page，一个对象可以保存在多个page上面，但一个page上不能被多个对象共享，vm-page-size是要根据存储的 数据大小来设定的，作者建议如果存储很多小对象，page大小最好设置为32或者64bytes；如果存储很大大对象，则可以使用更大的page，如果不 确定，就使用默认值</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vm-page-size 32</div></pre></td></tr></table></figure>
<ol>
<li>设置swap文件中的page数量，由于页表（一种表示页面空闲或使用的bitmap）是在放在内存中的，，在磁盘上每8个pages将消耗1byte的内存。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vm-pages 134217728</div></pre></td></tr></table></figure>
<ol>
<li>设置访问swap文件的线程数,最好不要超过机器的核数,如果设置为0,那么所有对swap文件的操作都是串行的，可能会造成比较长时间的延迟。默认值为4</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vm-max-threads 4</div></pre></td></tr></table></figure>
<ol>
<li>设置在向客户端应答时，是否把较小的包合并为一个包发送，默认为开启</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">glueoutputbuf yes</div></pre></td></tr></table></figure>
<ol>
<li>指定在超过一定的数量或者最大的元素超过某一临界值时，采用一种特殊的哈希算法</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">hash</span>-max-zipmap-entries 64</div><div class="line"><span class="built_in">hash</span>-max-zipmap-value 512</div></pre></td></tr></table></figure>
<ol>
<li>指定是否激活重置哈希，默认为开启（后面在介绍Redis的哈希算法时具体介绍）</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">activerehashing yes</div></pre></td></tr></table></figure>
<ol>
<li>指定包含其它的配置文件，可以在同一主机上多个Redis实例之间使用同一份配置文件，而同时各个实例又拥有自己的特定配置文件</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">include /path/to/local.conf</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/23. Redis 安装/">Redis 安装</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<h2 id="Window-下安装"><a href="#Window-下安装" class="headerlink" title="Window 下安装"></a>Window 下安装</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载地址：<a href="https://github.com/MSOpenTech/redis/releases。" target="_blank" rel="external">https://github.com/MSOpenTech/redis/releases。</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis 支持 32 位和 64 位。这个需要根据你系统平台的实际情况选择，这里我们下 载 <strong>Redis-x64-xxx.zip</strong>压缩包到 C 盘，解压后，将文件夹重新命名为 <strong>redis</strong>。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Redis%20%E5%AE%89%E8%A3%85/01.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;打开一个 <strong>cmd</strong> 窗口 使用cd命令切换目录到 <strong>C:\redis</strong> 运行 <strong>redis-server.exe redis.windows.conf</strong> 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果想方便的话，可以把 redis 的路径加到系统的环境变量里，这样就省得再输路径了，后面的那个 redis.windows.conf 可以省略，如果省略，会启用默认的。输入之后，会显示如下界面：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Redis%20%E5%AE%89%E8%A3%85/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这时候另启一个cmd窗口，原来的不要关闭，不然就无法访问服务端了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;切换到redis目录下运行 <strong>redis-cli.exe -h 127.0.0.1 -p 6379</strong> 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置键值对 <strong>set myKey abc</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;取出键值对 <strong>get myKey</strong></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Redis%20%E5%AE%89%E8%A3%85/03.jpeg?raw=true" alt=""></figure></p>
<h2 id="Linux-下安装"><a href="#Linux-下安装" class="headerlink" title="Linux 下安装"></a>Linux 下安装</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载地址：<a href="http://redis.io/download，下载最新文档版本。" target="_blank" rel="external">http://redis.io/download，下载最新文档版本。</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载并安装：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz</div><div class="line">$ tar xzf redis-2.8.17.tar.gz</div><div class="line">$ <span class="built_in">cd</span> redis-2.8.17</div><div class="line">$ make</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;make完后 redis-2.8.17目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli,两个程序位于安装目录 src 目录下：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面启动redis服务.</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">$ <span class="built_in">cd</span> src</div><div class="line">$ ./redis-server</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意这种方式启动redis 使用的是默认配置。也可以通过启动参数告诉redis使用指定配置文件使用下面命令启动。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">$ <span class="built_in">cd</span> src</div><div class="line">$ ./redis-server redis.conf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis.conf是一个默认的配置文件。我们可以根据需要使用自己的配置文件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动redis服务进程后，就可以使用测试客户端程序redis-cli和redis服务交互了。 比如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">$ <span class="built_in">cd</span> src</div><div class="line">$ ./redis-cli</div><div class="line">redis&gt; <span class="built_in">set</span> foo bar</div><div class="line">OK</div><div class="line">redis&gt; get foo</div><div class="line"><span class="string">"bar"</span></div></pre></td></tr></table></figure>
<h2 id="Ubuntu-下安装"><a href="#Ubuntu-下安装" class="headerlink" title="Ubuntu 下安装"></a>Ubuntu 下安装</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 Ubuntu 系统安装 Redi 可以使用以下命令:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$sudo</span> apt-get update</div><div class="line"><span class="variable">$sudo</span> apt-get install redis-server</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动 Redis</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ redis-server</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看 redis 是否启动？</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ redis-cli</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上命令将打开以下终端：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;127.0.0.1 是本机 IP ，6379 是 redis 服务端口。现在我们输入 PING 命令。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ping</div><div class="line">PONG</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上说明我们已经成功安装了redis。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/21. Redis 集合(Set)/">Redis 集合(Set)</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis的Set是string类型的无序集合。集合成员是唯一的，这就意味着集合中不能出现重复的数据。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis 中 集合是通过哈希表实现的，所以添加，删除，查找的复杂度都是O(1)。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。</p>
<h2 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">edis 127.0.0.1:6379&gt; SADD runoobkey redis</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD runoobkey mongodb</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD runoobkey mysql</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD runoobkey mysql</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis 127.0.0.1:6379&gt; SMEMBERS runoobkey</div><div class="line"> </div><div class="line">1) <span class="string">"mysql"</span></div><div class="line">2) <span class="string">"mongodb"</span></div><div class="line">3) <span class="string">"redis"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在以上实例中我们通过 <strong>SADD</strong> 命令向名为 <strong>runoobkey</strong> 的集合插入的三个元素。</p>
<h2 id="Redis-集合命令"><a href="#Redis-集合命令" class="headerlink" title="Redis 集合命令"></a>Redis 集合命令</h2><h3 id="1-Redis-Sadd-命令"><a href="#1-Redis-Sadd-命令" class="headerlink" title="1.Redis Sadd 命令"></a>1.Redis Sadd 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SADD key member1 [member2]</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>向集合添加一个或多个成员</code><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Sadd 命令将一个或多个成员元素加入到集合中，已经存在于集合的成员元素将被忽略。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;假如集合 key 不存在，则创建一个只包含添加的元素作成员的集合。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当集合 key 不是集合类型时，返回一个错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：在Redis2.4版本以前， SADD 只接受单个成员值。</p>
<h4 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Sadd 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD KEY_NAME VALUE1..VALUEN</div></pre></td></tr></table></figure>
<h4 id="可用版本"><a href="#可用版本" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值"><a href="#返回值" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;被添加到集合中的新元素的数量，不包括被忽略的元素。</p>
<h4 id="实例-1"><a href="#实例-1" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis 127.0.0.1:6379&gt; SMEMBERS myset</div><div class="line">1) <span class="string">"hello"</span></div><div class="line">2) <span class="string">"foo"</span></div></pre></td></tr></table></figure>
<h3 id="2-Redis-Scard-命令"><a href="#2-Redis-Scard-命令" class="headerlink" title="2.Redis Scard 命令"></a>2.Redis Scard 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SCARD key</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取集合的成员数</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Scard 命令返回集合中元素的数量。</p>
<h4 id="语法-1"><a href="#语法-1" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Scard 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SCARD KEY_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-1"><a href="#可用版本-1" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-1"><a href="#返回值-1" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;集合的数量。 当集合 key 不存在时，返回 0 。</p>
<h4 id="实例-2"><a href="#实例-2" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis 127.0.0.1:6379&gt; SCARD myset</div><div class="line">(<span class="built_in">integer</span>) 2</div></pre></td></tr></table></figure>
<h3 id="3-Redis-Sdiff-命令"><a href="#3-Redis-Sdiff-命令" class="headerlink" title="3.Redis Sdiff 命令"></a>3.Redis Sdiff 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SDIFF key1 [key2]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>返回给定所有集合的差集</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Sdiff 命令返回给定集合之间的差集。不存在的集合 key 将视为空集。</p>
<h4 id="语法-2"><a href="#语法-2" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Sdiff 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SDIFF FIRST_KEY OTHER_KEY1..OTHER_KEYN</div></pre></td></tr></table></figure>
<h4 id="可用版本-2"><a href="#可用版本-2" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-2"><a href="#返回值-2" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;包含差集成员的列表。</p>
<h4 id="实例-3"><a href="#实例-3" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset2 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset2 <span class="string">"world"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SDIFF myset myset2</div><div class="line">1) <span class="string">"foo"</span></div><div class="line">2) <span class="string">"bar"</span></div></pre></td></tr></table></figure>
<h3 id="4-Redis-Sdiffstore-命令"><a href="#4-Redis-Sdiffstore-命令" class="headerlink" title="4.Redis Sdiffstore 命令"></a>4.Redis Sdiffstore 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SDIFFSTORE destination key1 [key2]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>返回给定所有集合的差集并存储在 destination 中</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Sdiffstore 命令将给定集合之间的差集存储在指定的集合中。如果指定的集合 key 已存在，则会被覆盖。</p>
<h4 id="语法-3"><a href="#语法-3" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Sdiffstore 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SDIFFSTORE DESTINATION_KEY KEY1..KEYN</div></pre></td></tr></table></figure>
<h4 id="可用版本-3"><a href="#可用版本-3" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-3"><a href="#返回值-3" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;结果集中的元素数量。</p>
<h4 id="实例-4"><a href="#实例-4" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset2 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset2 <span class="string">"world"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SDIFFSTORE destset myset myset2</div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; SMEMBERS destset</div><div class="line">1) <span class="string">"foo"</span></div><div class="line">2) <span class="string">"bar"</span></div></pre></td></tr></table></figure>
<h3 id="5-Redis-Sinter-命令"><a href="#5-Redis-Sinter-命令" class="headerlink" title="5.Redis Sinter 命令"></a>5.Redis Sinter 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SINTER key1 [key2]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>返回给定所有集合的交集</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Sinter 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时，结果也为空集(根据集合运算定律)。</p>
<h4 id="语法-4"><a href="#语法-4" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Sinter 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SINTER KEY KEY1..KEYN</div></pre></td></tr></table></figure>
<h4 id="可用版本-4"><a href="#可用版本-4" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-4"><a href="#返回值-4" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;交集成员的列表。</p>
<h4 id="实例-5"><a href="#实例-5" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset2 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset2 <span class="string">"world"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SINTER myset myset2</div><div class="line">1) <span class="string">"hello"</span></div></pre></td></tr></table></figure>
<h3 id="6-Redis-Sinterstore-命令"><a href="#6-Redis-Sinterstore-命令" class="headerlink" title="6.Redis Sinterstore 命令"></a>6.Redis Sinterstore 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SINTERSTORE destination key1 [key2]</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>返回给定所有集合的交集并存储在 destination 中</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Sinterstore 命令将给定集合之间的交集存储在指定的集合中。如果指定的集合已经存在，则将其覆盖。</p>
<h4 id="语法-5"><a href="#语法-5" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Sinterstore 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SINTERSTORE DESTINATION_KEY KEY KEY1..KEYN</div></pre></td></tr></table></figure>
<h4 id="可用版本-5"><a href="#可用版本-5" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-5"><a href="#返回值-5" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;交集成员的列表。</p>
<h4 id="实例-6"><a href="#实例-6" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset2 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset2 <span class="string">"world"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SINTERSTORE myset myset1 myset2</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SMEMBERS myset</div><div class="line">1) <span class="string">"hello"</span></div></pre></td></tr></table></figure>
<h3 id="7-Redis-Sismember-命令"><a href="#7-Redis-Sismember-命令" class="headerlink" title="7.Redis Sismember 命令"></a>7.Redis Sismember 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SISMEMBER key member</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>判断 member 元素是否是集合 key 的成员</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Sismember 命令判断成员元素是否是集合的成员。</p>
<h4 id="语法-6"><a href="#语法-6" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Sismember 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SISMEMBER KEY VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-6"><a href="#可用版本-6" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-6"><a href="#返回值-6" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果成员元素是集合的成员，返回 1 。 如果成员元素不是集合的成员，或 key 不存在，返回 0 。</p>
<h4 id="实例-7"><a href="#实例-7" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SISMEMBER myset1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SISMEMBER myset1 <span class="string">"world"</span></div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="8-Redis-Smembers-命令"><a href="#8-Redis-Smembers-命令" class="headerlink" title="8.Redis Smembers 命令"></a>8.Redis Smembers 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SMEMBERS key</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>返回集合中的所有成员</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Smembers 命令返回集合中的所有的成员。 不存在的集合 key 被视为空集合。</p>
<h4 id="语法-7"><a href="#语法-7" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Smembers 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SMEMBERS KEY VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-7"><a href="#可用版本-7" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-7"><a href="#返回值-7" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;集合中的所有成员。</p>
<h4 id="实例-8"><a href="#实例-8" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"world"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SMEMBERS myset1</div><div class="line">1) <span class="string">"World"</span></div><div class="line">2) <span class="string">"Hello"</span></div></pre></td></tr></table></figure>
<h3 id="9-Redis-Smove-命令"><a href="#9-Redis-Smove-命令" class="headerlink" title="9.Redis Smove 命令"></a>9.Redis Smove 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SMOVE source destination member</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>将 member 元素从 source 集合移动到 destination 集合</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Smove 命令将指定成员 member 元素从 source 集合移动到 destination 集合。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SMOVE 是原子性操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果 source 集合不存在或不包含指定的 member 元素，则 SMOVE 命令不执行任何操作，仅返回 0 。否则， member 元素从 source 集合中被移除，并添加到 destination 集合中去。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当 destination 集合已经包含 member 元素时， SMOVE 命令只是简单地将 source 集合中的 member 元素删除。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当 source 或 destination 不是集合类型时，返回一个错误。</p>
<h4 id="语法-8"><a href="#语法-8" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Smove 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SMOVE SOURCE DESTINATION MEMBER</div></pre></td></tr></table></figure>
<h4 id="可用版本-8"><a href="#可用版本-8" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-8"><a href="#返回值-8" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果成员元素被成功移除，返回 1 。 如果成员元素不是 source 集合的成员，并且没有任何操作对 destination 集合执行，那么返回 0 。</p>
<h4 id="实例-9"><a href="#实例-9" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"world"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset2 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SMOVE myset1 myset2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SMEMBERS myset1</div><div class="line">1) <span class="string">"World"</span></div><div class="line">2) <span class="string">"Hello"</span></div><div class="line">redis 127.0.0.1:6379&gt; SMEMBERS myset2</div><div class="line">1) <span class="string">"foo"</span></div><div class="line">2) <span class="string">"bar"</span></div></pre></td></tr></table></figure>
<h3 id="10-Redis-Spop-命令"><a href="#10-Redis-Spop-命令" class="headerlink" title="10.Redis Spop 命令"></a>10.Redis Spop 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SPOP key</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>移除并返回集合中的一个随机元素</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Spop 命令用于移除并返回集合中的一个随机元素。</p>
<h4 id="语法-9"><a href="#语法-9" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Spop 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SPOP KEY</div></pre></td></tr></table></figure>
<h4 id="可用版本-9"><a href="#可用版本-9" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-9"><a href="#返回值-9" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;被移除的随机元素。 当集合不存在或是空集时，返回 nil 。</p>
<h4 id="实例-10"><a href="#实例-10" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"world"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SPOP myset1</div><div class="line"><span class="string">"bar"</span></div><div class="line">redis 127.0.0.1:6379&gt; SMEMBERS myset1</div><div class="line">1) <span class="string">"Hello"</span></div><div class="line">2) <span class="string">"world"</span></div></pre></td></tr></table></figure>
<h3 id="11-Redis-Srandmember-命令"><a href="#11-Redis-Srandmember-命令" class="headerlink" title="11.Redis Srandmember 命令"></a>11.Redis Srandmember 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SRANDMEMBER key [count]</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>返回集合中一个或多个随机数</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Srandmember 命令用于返回集合中的一个随机元素。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从 Redis 2.6 版本开始， Srandmember 命令接受可选的 count 参数：</p>
<ul>
<li>如果 count 为正数，且小于集合基数，那么命令返回一个包含 count 个元素的数组，数组中的元素各不相同。如果 count 大于等于集合基数，那么返回整个集合。</li>
<li>如果 count 为负数，那么命令返回一个数组，数组中的元素可能会重复出现多次，而数组的长度为 count 的绝对值。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该操作和 SPOP 相似，但 SPOP 将随机元素从集合中移除并返回，而 Srandmember 则   仅仅返回随机元素，而不对集合进行任何改动。</p>
<h4 id="语法-10"><a href="#语法-10" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Srandmember 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SRANDMEMBER KEY [count]</div></pre></td></tr></table></figure>
<h4 id="可用版本-10"><a href="#可用版本-10" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-10"><a href="#返回值-10" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;只提供集合 key 参数时，返回一个元素；如果集合为空，返回 nil 。 如果提供了 count 参数，那么返回一个数组；如果集合为空，返回空数组。</p>
<h4 id="实例-11"><a href="#实例-11" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"world"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SRANDMEMBER myset1</div><div class="line"><span class="string">"bar"</span></div><div class="line">redis 127.0.0.1:6379&gt; SRANDMEMBER myset1 2</div><div class="line">1) <span class="string">"Hello"</span></div><div class="line">2) <span class="string">"world"</span></div></pre></td></tr></table></figure>
<h3 id="12-Redis-Srem-命令"><a href="#12-Redis-Srem-命令" class="headerlink" title="12.Redis Srem 命令"></a>12.Redis Srem 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SREM key member1 [member2]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>移除集合中一个或多个成员</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>Redis Srem 命令用于移除集合中的一个或多个成员元素，不存在的成员元素会被忽略。</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当 key 不是集合类型，返回一个错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 Redis 2.4 版本以前， SREM 只接受单个成员值。</p>
<h4 id="语法-11"><a href="#语法-11" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Srem 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SREM KEY MEMBER1..MEMBERN</div></pre></td></tr></table></figure>
<h4 id="可用版本-11"><a href="#可用版本-11" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-11"><a href="#返回值-11" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;被成功移除的元素的数量，不包括被忽略的元素。</p>
<h4 id="实例-12"><a href="#实例-12" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"world"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SREM myset1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SREM myset1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis 127.0.0.1:6379&gt; SMEMBERS myset1</div><div class="line">1) <span class="string">"bar"</span></div><div class="line">2) <span class="string">"world"</span></div></pre></td></tr></table></figure>
<h3 id="13-Redis-Sunion-命令"><a href="#13-Redis-Sunion-命令" class="headerlink" title="13.Redis Sunion 命令"></a>13.Redis Sunion 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SUNION key1 [key2]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>返回所有给定集合的并集</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Sunion 命令返回给定集合的并集。不存在的集合 key 被视为空集。</p>
<h4 id="语法-12"><a href="#语法-12" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Sunion 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SUNION KEY KEY1..KEYN</div></pre></td></tr></table></figure>
<h4 id="可用版本-12"><a href="#可用版本-12" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-12"><a href="#返回值-12" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;并集成员的列表。</p>
<h4 id="实例-13"><a href="#实例-13" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"world"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset2 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SUNION myset1 myset2</div><div class="line">1) <span class="string">"bar"</span></div><div class="line">2) <span class="string">"world"</span></div><div class="line">3) <span class="string">"hello"</span></div><div class="line">4) <span class="string">"foo"</span></div></pre></td></tr></table></figure>
<h3 id="14-Redis-Sunionstore-命令"><a href="#14-Redis-Sunionstore-命令" class="headerlink" title="14.Redis Sunionstore 命令"></a>14.Redis Sunionstore 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SUNIONSTORE destination key1 [key2]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>所有给定集合的并集存储在 destination 集合中</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 中。如果 destination 已经存在，则将其覆盖。</p>
<h4 id="语法-13"><a href="#语法-13" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Sunionstore 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SUNIONSTORE DESTINATION KEY KEY1..KEYN</div></pre></td></tr></table></figure>
<h4 id="可用版本-13"><a href="#可用版本-13" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-13"><a href="#返回值-13" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;结果集中的元素数量。</p>
<h4 id="实例-14"><a href="#实例-14" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"world"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset2 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SUNIONSTORE myset myset1 myset2</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; SMEMBERS myset</div><div class="line">1) <span class="string">"bar"</span></div><div class="line">2) <span class="string">"world"</span></div><div class="line">3) <span class="string">"hello"</span></div><div class="line">4) <span class="string">"foo"</span></div></pre></td></tr></table></figure>
<h3 id="15-Redis-Sscan-命令"><a href="#15-Redis-Sscan-命令" class="headerlink" title="15.Redis Sscan 命令"></a>15.Redis Sscan 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SSCAN key cursor [MATCH pattern] [COUNT count]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>迭代集合中的元素</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Sscan 命令用于迭代集合键中的元素。</p>
<h4 id="语法-14"><a href="#语法-14" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Sscan 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SSCAN KEY [MATCH pattern] [COUNT count]</div></pre></td></tr></table></figure>
<h4 id="可用版本-14"><a href="#可用版本-14" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-14"><a href="#返回值-14" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;数组列表。</p>
<h4 id="实例-15"><a href="#实例-15" class="headerlink" title="实例"></a>实例</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 &quot;hello&quot;</div><div class="line">(integer) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 &quot;hi&quot;</div><div class="line">(integer) 1</div><div class="line">redis 127.0.0.1:6379&gt; SADD myset1 &quot;bar&quot;</div><div class="line">(integer) 1</div><div class="line">redis 127.0.0.1:6379&gt; sscan myset1 0 match h*</div><div class="line">1) &quot;0&quot;</div><div class="line">2) 1) &quot;hello&quot;</div><div class="line"> 2) &quot;h1&quot;</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/22. Redis 有序集合(sorted set)/">Redis 有序集合(sorted set)</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有序集合的成员是唯一的,但分数(score)却可以重复。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;集合是通过哈希表实现的，所以添加，删除，查找的复杂度都是O(1)。 集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。</p>
<h2 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZADD runoobkey 1 redis</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD runoobkey 2 mongodb</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD runoobkey 3 mysql</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD runoobkey 3 mysql</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis 127.0.0.1:6379&gt; ZADD runoobkey 4 mysql</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis 127.0.0.1:6379&gt; ZRANGE runoobkey 0 10 WITHSCORES</div><div class="line"> </div><div class="line">1) <span class="string">"redis"</span></div><div class="line">2) <span class="string">"1"</span></div><div class="line">3) <span class="string">"mongodb"</span></div><div class="line">4) <span class="string">"2"</span></div><div class="line">5) <span class="string">"mysql"</span></div><div class="line">6) <span class="string">"4"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在以上实例中我们通过命令 <strong>ZADD</strong> 向 redis 的有序集合中添加了三个值并关联上分数。</p>
<h2 id="Redis-有序集合命令"><a href="#Redis-有序集合命令" class="headerlink" title="Redis 有序集合命令"></a>Redis 有序集合命令</h2><h3 id="1-Redis-Zadd-命令"><a href="#1-Redis-Zadd-命令" class="headerlink" title="1.Redis Zadd 命令"></a>1.Redis Zadd 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZADD key score1 member1 [score2 member2]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>向有序集合添加一个或多个成员，或者更新已存在成员的分数</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zadd 命令用于将一个或多个成员元素及其分数值加入到有序集当中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果某个成员已经是有序集的成员，那么更新这个成员的分数值，并通过重新插入这个成员元素，来保证该成员在正确的位置上。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;分数值可以是整数值或双精度浮点数。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果有序集合 key 不存在，则创建一个空的有序集并执行 ZADD 操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当 key 存在但不是有序集类型时，返回一个错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意： 在 Redis 2.4 版本以前， ZADD 每次只能添加一个元素。</p>
<h4 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zadd 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZADD KEY_NAME SCORE1 VALUE1.. SCOREN VALUEN</div></pre></td></tr></table></figure>
<h4 id="可用版本"><a href="#可用版本" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.2.0</p>
<h4 id="返回值"><a href="#返回值" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;被成功添加的新成员的数量，不包括那些被更新的、已经存在的成员。</p>
<h4 id="实例-1"><a href="#实例-1" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZADD myset 1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD myset 1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD myset 2 <span class="string">"world"</span> 3 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; ZRANGE myzset 0 -1 WITHSCORES</div><div class="line">1) <span class="string">"hello"</span></div><div class="line">2) <span class="string">"1"</span></div><div class="line">3) <span class="string">"foo"</span></div><div class="line">4) <span class="string">"1"</span></div><div class="line">5) <span class="string">"world"</span></div><div class="line">6) <span class="string">"2"</span></div><div class="line">7) <span class="string">"bar"</span></div><div class="line">8) <span class="string">"3"</span></div></pre></td></tr></table></figure>
<h3 id="2-Redis-Zcard-命令"><a href="#2-Redis-Zcard-命令" class="headerlink" title="2.Redis Zcard 命令"></a>2.Redis Zcard 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZCARD key</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取有序集合的成员数</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zcard 命令用于计算集合中元素的数量。</p>
<h4 id="语法-1"><a href="#语法-1" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zcard 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZCARD KEY_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-1"><a href="#可用版本-1" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.2.0</p>
<h4 id="返回值-1"><a href="#返回值-1" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当 key 存在且是有序集类型时，返回有序集的基数。 当 key 不存在时，返回 0 。</p>
<h4 id="实例-2"><a href="#实例-2" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZADD myset 1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD myset 1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD myset 2 <span class="string">"world"</span> 3 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; ZCARD myzset</div><div class="line">(<span class="built_in">integer</span>) 4</div></pre></td></tr></table></figure>
<h3 id="3-Redis-Zcount-命令"><a href="#3-Redis-Zcount-命令" class="headerlink" title="3.Redis Zcount 命令"></a>3.Redis Zcount 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZCOUNT key min max</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>计算在有序集合中指定区间分数的成员数</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zcount 命令用于计算有序集合中指定分数区间的成员数量。</p>
<h4 id="语法-2"><a href="#语法-2" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zcount 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZCOUNT key min max</div></pre></td></tr></table></figure>
<h4 id="可用版本-2"><a href="#可用版本-2" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-2"><a href="#返回值-2" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;分数值在 min 和 max 之间的成员的数量。</p>
<h4 id="实例-3"><a href="#实例-3" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZADD myzset 1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD myzset 1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD myzset 2 <span class="string">"world"</span> 3 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; ZCOUNT myzset 1 3</div><div class="line">(<span class="built_in">integer</span>) 4</div></pre></td></tr></table></figure>
<h3 id="4-Redis-Zincrby-命令"><a href="#4-Redis-Zincrby-命令" class="headerlink" title="4.Redis Zincrby 命令"></a>4.Redis Zincrby 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZINCRBY key increment member</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>有序集合中对指定成员的分数加上增量 increment</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zincrby 命令对有序集合中指定成员的分数加上增量 increment</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以通过传递一个负数值 increment ，让分数减去相应的值，比如 ZINCRBY key -5  member ，就是让 member 的 score 值减去 5 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当 key 不存在，或分数不是 key 的成员时， ZINCRBY key increment member 等同于 ZADD key increment member 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当 key 不是有序集类型时，返回一个错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;分数值可以是整数值或双精度浮点数。</p>
<h4 id="语法-3"><a href="#语法-3" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zincrby 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZINCRBY key increment member</div></pre></td></tr></table></figure>
<h4 id="可用版本-3"><a href="#可用版本-3" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.2.0</p>
<h4 id="返回值-3"><a href="#返回值-3" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;member 成员的新分数值，以字符串形式表示。</p>
<h4 id="实例-4"><a href="#实例-4" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">edis 127.0.0.1:6379&gt; ZADD myzset 1 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD myzset 1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZINCRBY myzset 2 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line">redis 127.0.0.1:6379&gt; ZRANGE myzset 0 -1 WITHSCORES</div><div class="line">1) <span class="string">"foo"</span></div><div class="line">2) <span class="string">"2"</span></div><div class="line">3) <span class="string">"hello"</span></div><div class="line">4) <span class="string">"3"</span></div></pre></td></tr></table></figure>
<h3 id="5-Redis-Zinterstore-命令"><a href="#5-Redis-Zinterstore-命令" class="headerlink" title="5.Redis Zinterstore 命令"></a>5.Redis Zinterstore 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZINTERSTORE destination numkeys key [key ...]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zinterstore 命令计算给定的一个或多个有序集的交集，其中给定 key 的数量必须以 numkeys 参数指定，并将该交集(结果集)储存到 destination 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认情况下，结果集中某个成员的分数值是所有给定集下该成员分数值之和。</p>
<h4 id="语法-4"><a href="#语法-4" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zinterstore 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]</div></pre></td></tr></table></figure>
<h4 id="可用版本-4"><a href="#可用版本-4" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-4"><a href="#返回值-4" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存到目标结果集的的成员数量。</p>
<h4 id="实例-5"><a href="#实例-5" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 有序集 mid_test</span></div><div class="line">redis 127.0.0.1:6379&gt; ZADD mid_test 70 <span class="string">"Li Lei"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD mid_test 70 <span class="string">"Han Meimei"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD mid_test 99.5 <span class="string">"Tom"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line"><span class="comment"># 另一个有序集 fin_test</span></div><div class="line">redis 127.0.0.1:6379&gt; ZADD fin_test 88 <span class="string">"Li Lei"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD fin_test 75 <span class="string">"Han Meimei"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD fin_test 99.5 <span class="string">"Tom"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line"><span class="comment"># 交集</span></div><div class="line">redis 127.0.0.1:6379&gt; ZINTERSTORE sum_point 2 mid_test fin_test</div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line"> </div><div class="line"><span class="comment"># 显示有序集内所有成员及其分数值</span></div><div class="line">redis 127.0.0.1:6379&gt; ZRANGE sum_point 0 -1 WITHSCORES </div><div class="line">1) <span class="string">"Han Meimei"</span></div><div class="line">2) <span class="string">"145"</span></div><div class="line">3) <span class="string">"Li Lei"</span></div><div class="line">4) <span class="string">"158"</span></div><div class="line">5) <span class="string">"Tom"</span></div><div class="line">6) <span class="string">"199"</span></div></pre></td></tr></table></figure>
<h3 id="6-Redis-Zlexcount-命令"><a href="#6-Redis-Zlexcount-命令" class="headerlink" title="6.Redis Zlexcount 命令"></a>6.Redis Zlexcount 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZLEXCOUNT key min max</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>在有序集合中计算指定字典区间内成员数量</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zlexcount 命令在计算有序集合中指定字典区间内成员数量。</p>
<h4 id="语法-5"><a href="#语法-5" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zlexcount 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZLEXCOUNT KEY MIN MAX</div></pre></td></tr></table></figure>
<h4 id="可用版本-5"><a href="#可用版本-5" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.8.9</p>
<h4 id="返回值-5"><a href="#返回值-5" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定区间内的成员数量。</p>
<h4 id="实例-6"><a href="#实例-6" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZADD myzset 0 a 0 b 0 c 0 d 0 e</div><div class="line">(<span class="built_in">integer</span>) 5</div><div class="line">redis 127.0.0.1:6379&gt; ZADD myzset 0 f 0 g</div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; ZLEXCOUNT myzset - +</div><div class="line">(<span class="built_in">integer</span>) 7</div><div class="line">redis 127.0.0.1:6379&gt; ZLEXCOUNT myzset [b [f</div><div class="line">(<span class="built_in">integer</span>) 5</div></pre></td></tr></table></figure>
<h3 id="7-Redis-Zrange-命令"><a href="#7-Redis-Zrange-命令" class="headerlink" title="7.Redis Zrange 命令"></a>7.Redis Zrange 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZRANGE key start stop [WITHSCORES]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>通过索引区间返回有序集合成指定区间内的成员</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zrange 返回有序集中，指定区间内的成员。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中成员的位置按分数值递增(从小到大)来排序。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;具有相同分数值的成员按字典序(lexicographical order )来排列。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果你需要成员按值递减(从大到小)来排列，请使用 ZREVRANGE 命令。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下标参数 start 和 stop 都以 0 为底，也就是说，以 0 表示有序集第一个成员，以 1 表示有序集第二个成员，以此类推。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;你也可以使用负数下标，以 -1 表示最后一个成员， -2 表示倒数第二个成员，以此类推。</p>
<h4 id="语法-6"><a href="#语法-6" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zrange 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZRANGE key start stop [WITHSCORES]</div></pre></td></tr></table></figure>
<h4 id="可用版本-6"><a href="#可用版本-6" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.2.0</p>
<h4 id="返回值-6"><a href="#返回值-6" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定区间内，带有分数值(可选)的有序集成员的列表。</p>
<h4 id="实例-7"><a href="#实例-7" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZRANGE salary 0 -1 WITHSCORES <span class="comment"># 显示整个有序集成员</span></div><div class="line">1) <span class="string">"jack"</span></div><div class="line">2) <span class="string">"3500"</span></div><div class="line">3) <span class="string">"tom"</span></div><div class="line">4) <span class="string">"5000"</span></div><div class="line">5) <span class="string">"boss"</span></div><div class="line">6) <span class="string">"10086"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZRANGE salary 1 2 WITHSCORES <span class="comment"># 显示有序集下标区间 1 至 2 的成员</span></div><div class="line">1) <span class="string">"tom"</span></div><div class="line">2) <span class="string">"5000"</span></div><div class="line">3) <span class="string">"boss"</span></div><div class="line">4) <span class="string">"10086"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZRANGE salary 0 200000 WITHSCORES <span class="comment"># 测试 end 下标超出最大下标时的情况</span></div><div class="line">1) <span class="string">"jack"</span></div><div class="line">2) <span class="string">"3500"</span></div><div class="line">3) <span class="string">"tom"</span></div><div class="line">4) <span class="string">"5000"</span></div><div class="line">5) <span class="string">"boss"</span></div><div class="line">6) <span class="string">"10086"</span></div><div class="line"> </div><div class="line">redis &gt; ZRANGE salary 200000 3000000 WITHSCORES <span class="comment"># 测试当给定区间不存在于有序集时的情况</span></div><div class="line">(empty list or <span class="built_in">set</span>)</div></pre></td></tr></table></figure>
<h3 id="8-Redis-Zrangebylex-命令"><a href="#8-Redis-Zrangebylex-命令" class="headerlink" title="8.Redis Zrangebylex 命令"></a>8.Redis Zrangebylex 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZRANGEBYLEX key min max [LIMIT offset count]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>通过字典区间返回有序集合的成员</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zrangebylex 通过字典区间返回有序集合的成员。</p>
<h4 id="语法-7"><a href="#语法-7" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zrange 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZRANGEBYLEX key min max [LIMIT offset count]</div></pre></td></tr></table></figure>
<h4 id="可用版本-7"><a href="#可用版本-7" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.8.9</p>
<h4 id="返回值-7"><a href="#返回值-7" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定区间内的元素列表。</p>
<h4 id="实例-8"><a href="#实例-8" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZADD myzset 0 a 0 b 0 c 0 d 0 e 0 f 0 g</div><div class="line">(<span class="built_in">integer</span>) 7</div><div class="line">redis 127.0.0.1:6379&gt; ZRANGEBYLEX myzset - [c</div><div class="line">1) <span class="string">"a"</span></div><div class="line">2) <span class="string">"b"</span></div><div class="line">3) <span class="string">"c"</span></div><div class="line">redis 127.0.0.1:6379&gt; ZRANGEBYLEX myzset - (c</div><div class="line">1) <span class="string">"a"</span></div><div class="line">2) <span class="string">"b"</span></div><div class="line">redis 127.0.0.1:6379&gt; ZRANGEBYLEX myzset [aaa (g</div><div class="line">1) <span class="string">"b"</span></div><div class="line">2) <span class="string">"c"</span></div><div class="line">3) <span class="string">"d"</span></div><div class="line">4) <span class="string">"e"</span></div><div class="line">5) <span class="string">"f"</span></div><div class="line">redis&gt;</div></pre></td></tr></table></figure>
<h3 id="9-Redis-Zrangebyscore-命令"><a href="#9-Redis-Zrangebyscore-命令" class="headerlink" title="9.Redis Zrangebyscore 命令"></a>9.Redis Zrangebyscore 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>通过分数返回有序集合指定区间内的成员</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zrangebyscore 返回有序集合中指定分数区间的成员列表。有序集成员按分数值递增(从小到大)次序排列。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;具有相同分数值的成员按字典序来排列(该属性是有序集提供的，不需要额外的计算)。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认情况下，区间的取值使用闭区间 (小于等于或大于等于)，你也可以通过给参数前增加   ( 符号来使用可选的开区间 (小于或大于)。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;举个例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ZRANGEBYSCORE zset (1 5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回所有符合条件 1 &lt; score &lt;= 5 的成员，而</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ZRANGEBYSCORE zset (5 (10</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;则返回所有符合条件 5 &lt; score &lt; 10 的成员。</p>
<h4 id="语法-8"><a href="#语法-8" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zrangebyscore 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]</div></pre></td></tr></table></figure>
<h4 id="可用版本-8"><a href="#可用版本-8" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.5</p>
<h4 id="返回值-8"><a href="#返回值-8" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定区间内，带有分数值(可选)的有序集成员的列表。</p>
<h4 id="实例-9"><a href="#实例-9" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZADD salary 2500 jack <span class="comment"># 测试数据</span></div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis 127.0.0.1:6379&gt; ZADD salary 5000 tom</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis 127.0.0.1:6379&gt; ZADD salary 12000 peter</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZRANGEBYSCORE salary -inf +inf <span class="comment"># 显示整个有序集</span></div><div class="line">1) <span class="string">"jack"</span></div><div class="line">2) <span class="string">"tom"</span></div><div class="line">3) <span class="string">"peter"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZRANGEBYSCORE salary -inf +inf WITHSCORES <span class="comment"># 显示整个有序集及成员的 score 值</span></div><div class="line">1) <span class="string">"jack"</span></div><div class="line">2) <span class="string">"2500"</span></div><div class="line">3) <span class="string">"tom"</span></div><div class="line">4) <span class="string">"5000"</span></div><div class="line">5) <span class="string">"peter"</span></div><div class="line">6) <span class="string">"12000"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZRANGEBYSCORE salary -inf 5000 WITHSCORES <span class="comment"># 显示工资 &lt;=5000 的所有成员</span></div><div class="line">1) <span class="string">"jack"</span></div><div class="line">2) <span class="string">"2500"</span></div><div class="line">3) <span class="string">"tom"</span></div><div class="line">4) <span class="string">"5000"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZRANGEBYSCORE salary (5000 400000 <span class="comment"># 显示工资大于 5000 小于等于 400000 的成员</span></div><div class="line">1) <span class="string">"peter"</span></div></pre></td></tr></table></figure>
<h3 id="10-Redis-Zrank-命令"><a href="#10-Redis-Zrank-命令" class="headerlink" title="10.Redis Zrank 命令"></a>10.Redis Zrank 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZRANK key member</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>返回有序集合中指定成员的索引</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zrank 返回有序集中指定成员的排名。其中有序集成员按分数值递增(从小到大)顺序排列。</p>
<h4 id="语法-9"><a href="#语法-9" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zrank 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZRANK key member</div></pre></td></tr></table></figure>
<h4 id="可用版本-9"><a href="#可用版本-9" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-9"><a href="#返回值-9" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果成员是有序集 key 的成员，返回 member 的排名。 如果成员不是有序集 key 的成员，返回 nil 。</p>
<h4 id="实例-10"><a href="#实例-10" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZRANGE salary 0 -1 WITHSCORES <span class="comment"># 显示所有成员及其 score 值</span></div><div class="line">1) <span class="string">"peter"</span></div><div class="line">2) <span class="string">"3500"</span></div><div class="line">3) <span class="string">"tom"</span></div><div class="line">4) <span class="string">"4000"</span></div><div class="line">5) <span class="string">"jack"</span></div><div class="line">6) <span class="string">"5000"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZRANK salary tom <span class="comment"># 显示 tom 的薪水排名，第二</span></div><div class="line">(<span class="built_in">integer</span>) 1</div></pre></td></tr></table></figure>
<h3 id="11-Redis-Zrem-命令"><a href="#11-Redis-Zrem-命令" class="headerlink" title="11.Redis Zrem 命令"></a>11.Redis Zrem 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZREM key member [member ...]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>移除有序集合中的一个或多个成员</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zrem 命令用于移除有序集中的一个或多个成员，不存在的成员将被忽略。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当 key 存在但不是有序集类型时，返回一个错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意： 在 Redis 2.4 版本以前， ZREM 每次只能删除一个元素。</p>
<h4 id="语法-10"><a href="#语法-10" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zrem 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZREM key member [member ...]</div></pre></td></tr></table></figure>
<h4 id="可用版本-10"><a href="#可用版本-10" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.2.0</p>
<h4 id="返回值-10"><a href="#返回值-10" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;被成功移除的成员的数量，不包括被忽略的成员。</p>
<h4 id="实例-11"><a href="#实例-11" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 测试数据</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZRANGE page_rank 0 -1 WITHSCORES</div><div class="line">1) <span class="string">"bing.com"</span></div><div class="line">2) <span class="string">"8"</span></div><div class="line">3) <span class="string">"baidu.com"</span></div><div class="line">4) <span class="string">"9"</span></div><div class="line">5) <span class="string">"google.com"</span></div><div class="line">6) <span class="string">"10"</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 移除单个元素</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZREM page_rank google.com</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZRANGE page_rank 0 -1 WITHSCORES</div><div class="line">1) <span class="string">"bing.com"</span></div><div class="line">2) <span class="string">"8"</span></div><div class="line">3) <span class="string">"baidu.com"</span></div><div class="line">4) <span class="string">"9"</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 移除多个元素</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZREM page_rank baidu.com bing.com</div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZRANGE page_rank 0 -1 WITHSCORES</div><div class="line">(empty list or <span class="built_in">set</span>)</div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 移除不存在元素</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZREM page_rank non-exists-element</div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="12-Redis-Zremrangebylex-命令"><a href="#12-Redis-Zremrangebylex-命令" class="headerlink" title="12.Redis Zremrangebylex 命令"></a>12.Redis Zremrangebylex 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZREMRANGEBYLEX key min max</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>移除有序集合中给定的字典区间的所有成员</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zremrangebylex 命令用于移除有序集合中给定的字典区间的所有成员。</p>
<h4 id="语法-11"><a href="#语法-11" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zremrangebylex命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZREMRANGEBYLEX key min max</div></pre></td></tr></table></figure>
<h4 id="可用版本-11"><a href="#可用版本-11" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.8.9</p>
<h4 id="返回值-11"><a href="#返回值-11" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;被成功移除的成员的数量，不包括被忽略的成员。</p>
<h4 id="实例-12"><a href="#实例-12" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZADD myzset 0 aaaa 0 b 0 c 0 d 0 e</div><div class="line">(<span class="built_in">integer</span>) 5</div><div class="line">redis 127.0.0.1:6379&gt; ZADD myzset 0 foo 0 zap 0 zip 0 ALPHA 0 alpha</div><div class="line">(<span class="built_in">integer</span>) 5</div><div class="line">redis 127.0.0.1:6379&gt; ZRANGE myzset 0 -1</div><div class="line">1) <span class="string">"ALPHA"</span></div><div class="line"> 2) <span class="string">"aaaa"</span></div><div class="line"> 3) <span class="string">"alpha"</span></div><div class="line"> 4) <span class="string">"b"</span></div><div class="line"> 5) <span class="string">"c"</span></div><div class="line"> 6) <span class="string">"d"</span></div><div class="line"> 7) <span class="string">"e"</span></div><div class="line"> 8) <span class="string">"foo"</span></div><div class="line"> 9) <span class="string">"zap"</span></div><div class="line">10) <span class="string">"zip"</span></div><div class="line">redis 127.0.0.1:6379&gt; ZREMRANGEBYLEX myzset [alpha [omega</div><div class="line">(<span class="built_in">integer</span>) 6</div><div class="line">redis 127.0.0.1:6379&gt; ZRANGE myzset 0 -1</div><div class="line">1) <span class="string">"ALPHA"</span></div><div class="line">2) <span class="string">"aaaa"</span></div><div class="line">3) <span class="string">"zap"</span></div><div class="line">4) <span class="string">"zip"</span></div><div class="line">redis&gt;</div></pre></td></tr></table></figure>
<h3 id="13-Redis-Zremrangebyrank-命令"><a href="#13-Redis-Zremrangebyrank-命令" class="headerlink" title="13.Redis Zremrangebyrank 命令"></a>13.Redis Zremrangebyrank 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZREMRANGEBYRANK key start stop</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>移除有序集合中给定的排名区间的所有成员</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zremrangebyrank 命令用于移除有序集中，指定排名(rank)区间内的所有成员。</p>
<h4 id="语法-12"><a href="#语法-12" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zremrangebyrank 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZREMRANGEBYRANK key start stop</div></pre></td></tr></table></figure>
<h4 id="可用版本-12"><a href="#可用版本-12" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-12"><a href="#返回值-12" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;被移除成员的数量。</p>
<h4 id="实例-13"><a href="#实例-13" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZADD salary 2000 jack</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD salary 5000 tom</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD salary 3500 peter</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZREMRANGEBYRANK salary 0 1 <span class="comment"># 移除下标 0 至 1 区间内的成员</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZRANGE salary 0 -1 WITHSCORES <span class="comment"># 有序集只剩下一个成员</span></div><div class="line">1) <span class="string">"tom"</span></div><div class="line">2) <span class="string">"5000"</span></div></pre></td></tr></table></figure>
<h3 id="14-Redis-Zremrangebyscore-命令"><a href="#14-Redis-Zremrangebyscore-命令" class="headerlink" title="14.Redis Zremrangebyscore 命令"></a>14.Redis Zremrangebyscore 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZREMRANGEBYSCORE key min max</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>移除有序集合中给定的分数区间的所有成员</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zremrangebyscore 命令用于移除有序集中，指定分数（score）区间内的所有成员。</p>
<h4 id="语法-13"><a href="#语法-13" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zremrangebyscore 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZREMRANGEBYSCORE key min max</div></pre></td></tr></table></figure>
<h4 id="可用版本-13"><a href="#可用版本-13" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.2.0</p>
<h4 id="返回值-13"><a href="#返回值-13" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;被移除成员的数量。</p>
<h4 id="实例-14"><a href="#实例-14" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZRANGE salary 0 -1 WITHSCORES <span class="comment"># 显示有序集内所有成员及其 score 值</span></div><div class="line">1) <span class="string">"tom"</span></div><div class="line">2) <span class="string">"2000"</span></div><div class="line">3) <span class="string">"peter"</span></div><div class="line">4) <span class="string">"3500"</span></div><div class="line">5) <span class="string">"jack"</span></div><div class="line">6) <span class="string">"5000"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZREMRANGEBYSCORE salary 1500 3500 <span class="comment"># 移除所有薪水在 1500 到 3500 内的员工</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line"> </div><div class="line">redis&gt; ZRANGE salary 0 -1 WITHSCORES <span class="comment"># 剩下的有序集成员</span></div><div class="line">1) <span class="string">"jack"</span></div><div class="line">2) <span class="string">"5000"</span></div></pre></td></tr></table></figure>
<h3 id="15-Redis-Zrevrange-命令"><a href="#15-Redis-Zrevrange-命令" class="headerlink" title="15.Redis Zrevrange 命令"></a>15.Redis Zrevrange 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZREVRANGE key start stop [WITHSCORES]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>返回有序集中指定区间内的成员，通过索引，分数从高到底</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zrevrange 命令返回有序集中，指定区间内的成员。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中成员的位置按分数值递减(从大到小)来排列。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;具有相同分数值的成员按字典序的逆序(reverse lexicographical order)排列。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;除了成员按分数值递减的次序排列这一点外， ZREVRANGE 命令的其他方面和 ZRANGE 命令一样。</p>
<h4 id="语法-14"><a href="#语法-14" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zrevrange 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZREVRANGE key start stop [WITHSCORES]</div></pre></td></tr></table></figure>
<h4 id="可用版本-14"><a href="#可用版本-14" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.2.0</p>
<h4 id="返回值-14"><a href="#返回值-14" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定区间内，带有分数值(可选)的有序集成员的列表。</p>
<h4 id="实例-15"><a href="#实例-15" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZRANGE salary 0 -1 WITHSCORES <span class="comment"># 递增排列</span></div><div class="line">1) <span class="string">"peter"</span></div><div class="line">2) <span class="string">"3500"</span></div><div class="line">3) <span class="string">"tom"</span></div><div class="line">4) <span class="string">"4000"</span></div><div class="line">5) <span class="string">"jack"</span></div><div class="line">6) <span class="string">"5000"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZREVRANGE salary 0 -1 WITHSCORES <span class="comment"># 递减排列</span></div><div class="line">1) <span class="string">"jack"</span></div><div class="line">2) <span class="string">"5000"</span></div><div class="line">3) <span class="string">"tom"</span></div><div class="line">4) <span class="string">"4000"</span></div><div class="line">5) <span class="string">"peter"</span></div><div class="line">6) <span class="string">"3500"</span></div></pre></td></tr></table></figure>
<h3 id="16-Redis-Zrevrangebyscore-命令"><a href="#16-Redis-Zrevrangebyscore-命令" class="headerlink" title="16.Redis Zrevrangebyscore 命令"></a>16.Redis Zrevrangebyscore 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZREVRANGEBYSCORE key max min [WITHSCORES]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>返回有序集中指定分数区间内的成员，分数从高到低排序</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zrevrangebyscore 返回有序集中指定分数区间内的所有的成员。有序集成员按分数值递减(从大到小)的次序排列。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;具有相同分数值的成员按字典序的逆序(reverse lexicographical order )排列。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;除了成员按分数值递减的次序排列这一点外， ZREVRANGEBYSCORE 命令的其他方面 和 ZRANGEBYSCORE 命令一样。</p>
<h4 id="语法-15"><a href="#语法-15" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zrevrangebyscore 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]</div></pre></td></tr></table></figure>
<h4 id="可用版本-15"><a href="#可用版本-15" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.2.0</p>
<h4 id="返回值-15"><a href="#返回值-15" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定区间内，带有分数值(可选)的有序集成员的列表。</p>
<h4 id="实例-16"><a href="#实例-16" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZADD salary 10086 jack</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis &gt; ZADD salary 5000 tom</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD salary 7500 peter</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; ZADD salary 3500 joe</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZREVRANGEBYSCORE salary +inf -inf <span class="comment"># 逆序排列所有成员</span></div><div class="line">1) <span class="string">"jack"</span></div><div class="line">2) <span class="string">"peter"</span></div><div class="line">3) <span class="string">"tom"</span></div><div class="line">4) <span class="string">"joe"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZREVRANGEBYSCORE salary 10000 2000 <span class="comment"># 逆序排列薪水介于 10000 和 2000 之间的成员</span></div><div class="line">1) <span class="string">"peter"</span></div><div class="line">2) <span class="string">"tom"</span></div><div class="line">3) <span class="string">"joe"</span></div></pre></td></tr></table></figure>
<h3 id="17-Redis-Zrevrank-命令"><a href="#17-Redis-Zrevrank-命令" class="headerlink" title="17.Redis Zrevrank 命令"></a>17.Redis Zrevrank 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZREVRANK key member</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>返回有序集合中指定成员的排名，有序集成员按分数值递减(从大到小)排序</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zrevrank 命令返回有序集中成员的排名。其中有序集成员按分数值递减(从大到小)排序。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;排名以 0 为底，也就是说， 分数值最大的成员排名为 0 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 ZRANK 命令可以获得成员按分数值递增(从小到大)排列的排名。</p>
<h4 id="语法-16"><a href="#语法-16" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zrevrank 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZREVRANK key member</div></pre></td></tr></table></figure>
<h4 id="可用版本-16"><a href="#可用版本-16" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.2.0</p>
<h4 id="返回值-16"><a href="#返回值-16" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果成员是有序集 key 的成员，返回成员的排名。 如果成员不是有序集 key 的成员，返回 nil 。</p>
<h4 id="实例-17"><a href="#实例-17" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZRANGE salary 0 -1 WITHSCORES <span class="comment"># 测试数据</span></div><div class="line">1) <span class="string">"jack"</span></div><div class="line">2) <span class="string">"2000"</span></div><div class="line">3) <span class="string">"peter"</span></div><div class="line">4) <span class="string">"3500"</span></div><div class="line">5) <span class="string">"tom"</span></div><div class="line">6) <span class="string">"5000"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZREVRANK salary peter <span class="comment"># peter 的工资排第二</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZREVRANK salary tom <span class="comment"># tom 的工资最高</span></div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="18-Redis-Zscore-命令"><a href="#18-Redis-Zscore-命令" class="headerlink" title="18.Redis Zscore 命令"></a>18.Redis Zscore 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZSCORE key member</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>返回有序集中，成员的分数值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zscore 命令返回有序集中，成员的分数值。 如果成员元素不是有序集 key 的成员，或 key 不存在，返回 nil 。</p>
<h4 id="语法-17"><a href="#语法-17" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zscore 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZSCORE key member</div></pre></td></tr></table></figure>
<h4 id="可用版本-17"><a href="#可用版本-17" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.2.0</p>
<h4 id="返回值-17"><a href="#返回值-17" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;成员的分数值，以字符串形式表示。</p>
<h4 id="实例-18"><a href="#实例-18" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZRANGE salary 0 -1 WITHSCORES <span class="comment"># 测试数据</span></div><div class="line">1) <span class="string">"tom"</span></div><div class="line">2) <span class="string">"2000"</span></div><div class="line">3) <span class="string">"peter"</span></div><div class="line">4) <span class="string">"3500"</span></div><div class="line">5) <span class="string">"jack"</span></div><div class="line">6) <span class="string">"5000"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZSCORE salary peter <span class="comment"># 注意返回值是字符串</span></div><div class="line"><span class="string">"3500"</span></div></pre></td></tr></table></figure>
<h3 id="19-Redis-Zunionstore-命令"><a href="#19-Redis-Zunionstore-命令" class="headerlink" title="19.Redis Zunionstore 命令"></a>19.Redis Zunionstore 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZUNIONSTORE destination numkeys key [key ...]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>计算给定的一个或多个有序集的并集，并存储在新的 key 中</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zunionstore 命令计算给定的一个或多个有序集的并集，其中给定 key 的数量必须以 numkeys 参数指定，并将该并集(结果集)储存到 destination 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认情况下，结果集中某个成员的分数值是所有给定集下该成员分数值之和 。</p>
<h4 id="语法-18"><a href="#语法-18" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zunionstore 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]</div></pre></td></tr></table></figure>
<h4 id="可用版本-18"><a href="#可用版本-18" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-18"><a href="#返回值-18" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存到 destination 的结果集的成员数量。</p>
<h4 id="实例-19"><a href="#实例-19" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; ZRANGE programmer 0 -1 WITHSCORES</div><div class="line">1) <span class="string">"peter"</span></div><div class="line">2) <span class="string">"2000"</span></div><div class="line">3) <span class="string">"jack"</span></div><div class="line">4) <span class="string">"3500"</span></div><div class="line">5) <span class="string">"tom"</span></div><div class="line">6) <span class="string">"5000"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZRANGE manager 0 -1 WITHSCORES</div><div class="line">1) <span class="string">"herry"</span></div><div class="line">2) <span class="string">"2000"</span></div><div class="line">3) <span class="string">"mary"</span></div><div class="line">4) <span class="string">"3500"</span></div><div class="line">5) <span class="string">"bob"</span></div><div class="line">6) <span class="string">"4000"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZUNIONSTORE salary 2 programmer manager WEIGHTS 1 3 <span class="comment"># 公司决定加薪。。。除了程序员。。。</span></div><div class="line">(<span class="built_in">integer</span>) 6</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; ZRANGE salary 0 -1 WITHSCORES</div><div class="line">1) <span class="string">"peter"</span></div><div class="line">2) <span class="string">"2000"</span></div><div class="line">3) <span class="string">"jack"</span></div><div class="line">4) <span class="string">"3500"</span></div><div class="line">5) <span class="string">"tom"</span></div><div class="line">6) <span class="string">"5000"</span></div><div class="line">7) <span class="string">"herry"</span></div><div class="line">8) <span class="string">"6000"</span></div><div class="line">9) <span class="string">"mary"</span></div><div class="line">10) <span class="string">"10500"</span></div><div class="line">11) <span class="string">"bob"</span></div><div class="line">12) <span class="string">"12000"</span></div></pre></td></tr></table></figure>
<h3 id="20-Redis-Zscan-命令"><a href="#20-Redis-Zscan-命令" class="headerlink" title="20.Redis Zscan 命令"></a>20.Redis Zscan 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>ZSCAN key cursor [MATCH pattern] [COUNT count]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>迭代有序集合中的元素（包括元素成员和元素分值）</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Zscan 命令用于迭代有序集合中的元素（包括元素成员和元素分值）</p>
<h4 id="语法-19"><a href="#语法-19" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Zscan 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; redis 127.0.0.1:6379&gt; ZSCAN key cursor [MATCH pattern] [COUNT count]</div></pre></td></tr></table></figure>
<h4 id="可用版本-19"><a href="#可用版本-19" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.8.0</p>
<h4 id="返回值-19"><a href="#返回值-19" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回的每个元素都是一个有序集合元素，一个有序集合元素由一个成员（member）和一个分值（score）组成。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/20. Redis 列表(List)/">Redis 列表(List)</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis列表是简单的字符串列表，按照插入顺序排序。你可以添加一个元素导列表的头部（左边）或者尾部（右边）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个列表最多可以包含 232 - 1 个元素 (4294967295, 每个列表超过40亿个元素)。</p>
<h2 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; LPUSH runoobkey redis</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; LPUSH runoobkey mongodb</div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; LPUSH runoobkey mysql</div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line">redis 127.0.0.1:6379&gt; LRANGE runoobkey 0 10</div><div class="line"> </div><div class="line">1) <span class="string">"mysql"</span></div><div class="line">2) <span class="string">"mongodb"</span></div><div class="line">3) <span class="string">"redis"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在以上实例中我们使用了 <strong>LPUSH</strong> 将三个值插入了名为 <strong>runoobkey</strong> 的列表当中。</p>
<h2 id="Redis-列表命令"><a href="#Redis-列表命令" class="headerlink" title="Redis 列表命令"></a>Redis 列表命令</h2><h3 id="1-Redis-Blpop-命令"><a href="#1-Redis-Blpop-命令" class="headerlink" title="1.Redis Blpop 命令"></a>1.Redis Blpop 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>BLPOP key1 [key2 ] timeout</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>移出并获取列表的第一个元素， 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Blpop 命令移出并获取列表的第一个元素， 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</p>
<h4 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Blpop 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; BLPOP LIST1 LIST2 .. LISTN TIMEOUT</div></pre></td></tr></table></figure>
<h4 id="可用版本"><a href="#可用版本" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值"><a href="#返回值" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果列表为空，返回一个 nil 。 否则，返回一个含有两个元素的列表，第一个元素是被弹出元素所属的 key ，第二个元素是被弹出元素的值。</p>
<h4 id="实例-1"><a href="#实例-1" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; BLPOP list1 100</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在以上实例中，操作会被阻塞，如果指定的列表 key list1 存在数据则会返回第一个元素，否则在等待100秒后会返回 nil 。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">(nil)</div><div class="line">(100.06s)</div></pre></td></tr></table></figure>
<h3 id="2-Redis-Brpop-命令"><a href="#2-Redis-Brpop-命令" class="headerlink" title="2.Redis Brpop 命令"></a>2.Redis Brpop 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>BRPOP key1 [key2 ] timeout</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>移出并获取列表的最后一个元素， 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Brpop 命令移出并获取列表的最后一个元素， 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</p>
<h4 id="语法-1"><a href="#语法-1" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Blpop 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; BRPOP LIST1 LIST2 .. LISTN TIMEOUT</div></pre></td></tr></table></figure>
<h4 id="可用版本-1"><a href="#可用版本-1" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-1"><a href="#返回值-1" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;假如在指定时间内没有任何元素被弹出，则返回一个 nil 和等待时长。 反之，返回一个含有两个元素的列表，第一个元素是被弹出元素所属的 key ，第二个元素是被弹出元素的值。</p>
<h4 id="实例-2"><a href="#实例-2" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; BRPOP list1 100</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在以上实例中，操作会被阻塞，如果指定的列表 key list1 存在数据则会返回第一个元素，否则在等待100秒后会返回 nil 。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">(nil)</div><div class="line">(100.06s)</div></pre></td></tr></table></figure>
<h3 id="3-Redis-Brpoplpush-命令"><a href="#3-Redis-Brpoplpush-命令" class="headerlink" title="3.Redis Brpoplpush 命令"></a>3.Redis Brpoplpush 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>BRPOPLPUSH source destination timeout</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>从列表中弹出一个值，将弹出的元素插入到另外一个列表中并返回它；</code> 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Brpoplpush 命令从列表中弹出一个值，将弹出的元素插入到另外一个列表中并返回它； 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</p>
<h4 id="语法-2"><a href="#语法-2" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Brpoplpush 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; BRPOPLPUSH LIST1 ANOTHER_LIST TIMEOUT</div></pre></td></tr></table></figure>
<h4 id="可用版本-2"><a href="#可用版本-2" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-2"><a href="#返回值-2" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;假如在指定时间内没有任何元素被弹出，则返回一个 nil 和等待时长。 反之，返回一个含有两个元素的列表，第一个元素是被弹出元素的值，第二个元素是等待时长。</p>
<h4 id="实例-3"><a href="#实例-3" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 非空列表</span></div><div class="line"> </div><div class="line">redis&gt; BRPOPLPUSH msg reciver 500</div><div class="line"><span class="string">"hello moto"</span> <span class="comment"># 弹出元素的值</span></div><div class="line">(3.38s) <span class="comment"># 等待时长</span></div><div class="line"> </div><div class="line">redis&gt; LLEN reciver</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis&gt; LRANGE reciver 0 0</div><div class="line">1) <span class="string">"hello moto"</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 空列表</span></div><div class="line"> </div><div class="line">redis&gt; BRPOPLPUSH msg reciver 1</div><div class="line">(nil)</div><div class="line">(1.34s)</div></pre></td></tr></table></figure>
<h3 id="4-Redis-Lindex-命令"><a href="#4-Redis-Lindex-命令" class="headerlink" title="4.Redis Lindex 命令"></a>4.Redis Lindex 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>LINDEX key index</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>通过索引获取列表中的元素</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Lindex 命令用于通过索引获取列表中的元素。你也可以使用负数下标，以 -1 表示列表的最后一个元素， -2 表示列表的倒数第二个元素，以此类推。</p>
<h4 id="语法-3"><a href="#语法-3" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Lindex 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; LINDEX KEY_NAME INDEX_POSITION</div></pre></td></tr></table></figure>
<h4 id="可用版本-3"><a href="#可用版本-3" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-3"><a href="#返回值-3" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表中下标为指定索引值的元素。 如果指定索引值不在列表的区间范围内，返回 nil 。</p>
<h4 id="实例-4"><a href="#实例-4" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; LPUSH mylist <span class="string">"World"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; LPUSH mylist <span class="string">"Hello"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; LINDEX mylist 0</div><div class="line"><span class="string">"Hello"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; LINDEX mylist -1</div><div class="line"><span class="string">"World"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; LINDEX mylist 3 <span class="comment"># index不在 mylist 的区间范围内</span></div><div class="line">(nil)</div></pre></td></tr></table></figure>
<h3 id="5-Redis-Linsert-命令"><a href="#5-Redis-Linsert-命令" class="headerlink" title="5.Redis Linsert 命令"></a>5.Redis Linsert 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>LINSERT key BEFORE|AFTER pivot value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>在列表的元素前或者后插入元素</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Linsert 命令用于在列表的元素前或者后插入元素。 当指定元素不存在于列表中时，不执行任何操作。 当列表不存在时，被视为空列表，不执行任何操作。 如果 key 不是列表类型，返回一个错误。</p>
<h4 id="语法-4"><a href="#语法-4" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Linsert 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; LINSERT KEY_NAME BEFORE EXISTING_VALUE NEW_VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-4"><a href="#可用版本-4" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-4"><a href="#返回值-4" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果命令执行成功，返回插入操作完成之后，列表的长度。 如果没有找到指定元素 ，返回 -1 。 如果 key 不存在或为空列表，返回 0 。</p>
<h4 id="实例-5"><a href="#实例-5" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; RPUSH list1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH list1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; LINSERT list1 BEFORE <span class="string">"bar"</span> <span class="string">"Yes"</span></div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line">redis 127.0.0.1:6379&gt; LRANGE mylist 0 -1</div><div class="line">1) <span class="string">"foo"</span></div><div class="line">2) <span class="string">"Yes"</span></div><div class="line">3) <span class="string">"bar"</span></div></pre></td></tr></table></figure>
<h3 id="6-Redis-Llen-命令"><a href="#6-Redis-Llen-命令" class="headerlink" title="6.Redis Llen 命令"></a>6.Redis Llen 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>LLEN key</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取列表长度</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Llen 命令用于返回列表的长度。 如果列表 key 不存在，则 key 被解释为一个空列表，返回 0 。 如果 key 不是列表类型，返回一个错误。</p>
<h4 id="语法-5"><a href="#语法-5" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Llen 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; LLEN KEY_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-5"><a href="#可用版本-5" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-5"><a href="#返回值-5" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表的长度。</p>
<h4 id="实例-6"><a href="#实例-6" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; RPUSH list1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH list1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; LLEN list1</div><div class="line">(<span class="built_in">integer</span>) 2</div></pre></td></tr></table></figure>
<h3 id="7-Redis-Lpop-命令"><a href="#7-Redis-Lpop-命令" class="headerlink" title="7.Redis Lpop 命令"></a>7.Redis Lpop 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>LPOP key</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>移出并获取列表的第一个元素</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Lpop 命令用于移除并返回列表的第一个元素。</p>
<h4 id="语法-6"><a href="#语法-6" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Lpop 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; LLEN KEY_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-6"><a href="#可用版本-6" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-6"><a href="#返回值-6" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表的第一个元素。 当列表 key 不存在时，返回 nil 。</p>
<h4 id="实例-7"><a href="#实例-7" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; RPUSH list1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH list1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; LPOP list1</div><div class="line"><span class="string">"foo"</span></div></pre></td></tr></table></figure>
<h3 id="8-Redis-Lpush-命令"><a href="#8-Redis-Lpush-命令" class="headerlink" title="8.Redis Lpush 命令"></a>8.Redis Lpush 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>LPUSH key value1 [value2]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>将一个或多个值插入到列表头部</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Lpush 命令将一个或多个值插入到列表头部。 如果 key 不存在，一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时，返回一个错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：在Redis 2.4版本以前的 LPUSH 命令，都只接受单个 value 值。</p>
<h4 id="语法-7"><a href="#语法-7" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Lpush 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; LPUSH KEY_NAME VALUE1.. VALUEN</div></pre></td></tr></table></figure>
<h4 id="可用版本-7"><a href="#可用版本-7" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-7"><a href="#返回值-7" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行 LPUSH 命令后，列表的长度。</p>
<h4 id="实例-8"><a href="#实例-8" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; LPUSH list1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">127.0.0.1:6379&gt; LPUSH list1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">127.0.0.1:6379&gt; LRANGE list1 0 -1</div><div class="line">1) <span class="string">"bar"</span></div><div class="line">2) <span class="string">"foo"</span></div></pre></td></tr></table></figure>
<h3 id="9-Redis-Lpushx-命令"><a href="#9-Redis-Lpushx-命令" class="headerlink" title="9.Redis Lpushx 命令"></a>9.Redis Lpushx 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>LPUSHX key value</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>将一个或多个值插入到已存在的列表头部</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Lpushx 将一个或多个值插入到已存在的列表头部，列表不存在时操作无效。</p>
<h4 id="语法-8"><a href="#语法-8" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Lpushx 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; LPUSHX KEY_NAME VALUE1.. VALUEN</div></pre></td></tr></table></figure>
<h4 id="可用版本-8"><a href="#可用版本-8" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.2.0</p>
<h4 id="返回值-8"><a href="#返回值-8" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;LPUSHX 命令执行之后，列表的长度。</p>
<h4 id="实例-9"><a href="#实例-9" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; LPUSH list1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">127.0.0.1:6379&gt; LPUSHX list1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">127.0.0.1:6379&gt; LPUSHX list2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">127.0.0.1:6379&gt; LRANGE list1 0 -1</div><div class="line">1) <span class="string">"bar"</span></div><div class="line">2) <span class="string">"foo"</span></div></pre></td></tr></table></figure>
<h3 id="10-Redis-Lrange-命令"><a href="#10-Redis-Lrange-命令" class="headerlink" title="10.Redis Lrange 命令"></a>10.Redis Lrange 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>LRANGE key start stop</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取列表指定范围内的元素</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中 0 表示列表的第一个元素， 1 表示列表的第二个元素，以此类推。 你也可以使用负数下标，以 -1 表示列表的最后一个元素， -2 表示列表的倒数第二个元素，以此类推。</p>
<h4 id="语法-9"><a href="#语法-9" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Lrange 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; LRANGE KEY_NAME START END</div></pre></td></tr></table></figure>
<h4 id="可用版本-9"><a href="#可用版本-9" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-9"><a href="#返回值-9" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个列表，包含指定区间内的元素。</p>
<h4 id="实例-10"><a href="#实例-10" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; LPUSH list1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; LPUSH list1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; LPUSHX list1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis 127.0.0.1:6379&gt; LRANGE list1 0 -1</div><div class="line">1) <span class="string">"bar"</span></div><div class="line">2) <span class="string">"bar"</span></div><div class="line">3) <span class="string">"foo"</span></div></pre></td></tr></table></figure>
<h3 id="11-Redis-Lrem-命令"><a href="#11-Redis-Lrem-命令" class="headerlink" title="11.Redis Lrem 命令"></a>11.Redis Lrem 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>LREM key count value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>移除列表元素</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Lrem 根据参数 COUNT 的值，移除列表中与参数 VALUE 相等的元素。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;COUNT 的值可以是以下几种：</p>
<ul>
<li>count &gt; 0 : 从表头开始向表尾搜索，移除与 VALUE 相等的元素，数量为 COUNT 。</li>
<li>count &lt; 0 : 从表尾开始向表头搜索，移除与 VALUE 相等的元素，数量为 COUNT 的绝对值。</li>
<li>count = 0 : 移除表中所有与 VALUE 相等的值。</li>
</ul>
<h4 id="语法-10"><a href="#语法-10" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Lrem 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; LREM KEY_NAME COUNT VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-10"><a href="#可用版本-10" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-10"><a href="#返回值-10" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;被移除元素的数量。 列表不存在时返回 0 。</p>
<h4 id="实例-11"><a href="#实例-11" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 4</div><div class="line">redis 127.0.0.1:6379&gt; LREM mylist -2 <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div></pre></td></tr></table></figure>
<h3 id="12-Redis-Lset-命令"><a href="#12-Redis-Lset-命令" class="headerlink" title="12.Redis Lset 命令"></a>12.Redis Lset 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>LSET key index value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>通过索引设置列表元素的值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Lset 通过索引来设置元素的值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当索引参数超出范围，或对一个空列表进行 LSET 时，返回一个错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关于列表下标的更多信息，请参考 LINDEX 命令。</p>
<h4 id="语法-11"><a href="#语法-11" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Lset 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; LSET KEY_NAME INDEX VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-11"><a href="#可用版本-11" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-11"><a href="#返回值-11" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;操作成功返回 ok ，否则返回错误信息。</p>
<h4 id="实例-12"><a href="#实例-12" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 4</div><div class="line">redis 127.0.0.1:6379&gt; LSET mylist 0 <span class="string">"bar"</span></div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; LRANGE mylist 0 -1</div><div class="line">1: <span class="string">"bar"</span></div><div class="line">2) <span class="string">"hello"</span></div><div class="line">3) <span class="string">"foo"</span></div><div class="line">4) <span class="string">"hello"</span></div></pre></td></tr></table></figure>
<h3 id="13-Redis-Ltrim-命令"><a href="#13-Redis-Ltrim-命令" class="headerlink" title="13.Redis Ltrim 命令"></a>13.Redis Ltrim 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>LTRIM key start stop</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>对一个列表进行修剪(trim)，就是说，让列表只保留指定区间内的元素，不在指定区间之内的元素都将被删除</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Ltrim 对一个列表进行修剪(trim)，就是说，让列表只保留指定区间内的元素，不在指定区间之内的元素都将被删除。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下标 0 表示列表的第一个元素，以 1 表示列表的第二个元素，以此类推。 你也可以使用负数下标，以 -1 表示列表的最后一个元素， -2 表示列表的倒数第二个元素，以此类推。</p>
<h4 id="语法-12"><a href="#语法-12" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Ltrim 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; LTRIM KEY_NAME START STOP</div></pre></td></tr></table></figure>
<h4 id="可用版本-12"><a href="#可用版本-12" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-12"><a href="#返回值-12" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;命令执行成功时，返回 ok 。</p>
<h4 id="实例-13"><a href="#实例-13" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 4</div><div class="line">redis 127.0.0.1:6379&gt; LTRIM mylist 1 -1</div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; LRANGE mylist 0 -1</div><div class="line">1) <span class="string">"hello"</span></div><div class="line">2) <span class="string">"foo"</span></div><div class="line">3) <span class="string">"bar"</span></div></pre></td></tr></table></figure>
<h3 id="14-Redis-Rpop-命令"><a href="#14-Redis-Rpop-命令" class="headerlink" title="14.Redis Rpop 命令"></a>14.Redis Rpop 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>RPOP key</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>移除并获取列表最后一个元素</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Rpop 命令用于移除并返回列表的最后一个元素。</p>
<h4 id="语法-13"><a href="#语法-13" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Rpop 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; RPOP KEY_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-13"><a href="#可用版本-13" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-13"><a href="#返回值-13" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表的最后一个元素。 当列表不存在时，返回 nil 。</p>
<h4 id="实例-14"><a href="#实例-14" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 4</div><div class="line">redis 127.0.0.1:6379&gt; RPOP mylist</div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; LRANGE mylist 0 -1</div><div class="line">1) <span class="string">"hello"</span></div><div class="line">2) <span class="string">"hello"</span></div><div class="line">3) <span class="string">"foo"</span></div></pre></td></tr></table></figure>
<h3 id="15-Redis-Rpoplpush-命令"><a href="#15-Redis-Rpoplpush-命令" class="headerlink" title="15.Redis Rpoplpush 命令"></a>15.Redis Rpoplpush 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>RPOPLPUSH source destination</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>移除列表的最后一个元素，并将该元素添加到另一个列表并返回</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Rpoplpush 命令用于移除列表的最后一个元素，并将该元素添加到另一个列表并返回。</p>
<h4 id="语法-14"><a href="#语法-14" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Rpoplpush 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; RPOPLPUSH SOURCE_KEY_NAME DESTINATION_KEY_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-14"><a href="#可用版本-14" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-14"><a href="#返回值-14" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;被弹出的元素。</p>
<h4 id="实例-15"><a href="#实例-15" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line">redis 127.0.0.1:6379&gt; RPOPLPUSH mylist myotherlist</div><div class="line"><span class="string">"bar"</span></div><div class="line">redis 127.0.0.1:6379&gt; LRANGE mylist 0 -1</div><div class="line">1) <span class="string">"hello"</span></div><div class="line">2) <span class="string">"foo"</span></div></pre></td></tr></table></figure>
<h3 id="16-Redis-Rpush-命令"><a href="#16-Redis-Rpush-命令" class="headerlink" title="16.Redis Rpush 命令"></a>16.Redis Rpush 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>RPUSH key value1 [value2]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>在列表中添加一个或多个值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Rpush 命令用于将一个或多个值插入到列表的尾部(最右边)。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果列表不存在，一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时，返回一个错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：在 Redis 2.4 版本以前的 RPUSH 命令，都只接受单个 value 值。</p>
<h4 id="语法-15"><a href="#语法-15" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Rpush 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; RPUSH KEY_NAME VALUE1..VALUEN</div></pre></td></tr></table></figure>
<h4 id="可用版本-15"><a href="#可用版本-15" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-15"><a href="#返回值-15" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行 RPUSH 操作后，列表的长度。</p>
<h4 id="实例-16"><a href="#实例-16" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line">redis 127.0.0.1:6379&gt; LRANGE mylist 0 -1</div><div class="line">1) <span class="string">"hello"</span></div><div class="line">2) <span class="string">"foo"</span></div><div class="line">3) <span class="string">"bar"</span></div></pre></td></tr></table></figure>
<h3 id="17-Redis-Rpushx-命令"><a href="#17-Redis-Rpushx-命令" class="headerlink" title="17.Redis Rpushx 命令"></a>17.Redis Rpushx 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>RPUSHX key value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>为已存在的列表添加值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Rpushx 命令用于将一个或多个值插入到已存在的列表尾部(最右边)。如果列表不存在，操作无效。</p>
<h4 id="语法-16"><a href="#语法-16" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Rpushx 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; RPUSHX KEY_NAME VALUE1..VALUEN</div></pre></td></tr></table></figure>
<h4 id="可用版本-16"><a href="#可用版本-16" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.2.0</p>
<h4 id="返回值-16"><a href="#返回值-16" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行 Rpushx 操作后，列表的长度。</p>
<h4 id="实例-17"><a href="#实例-17" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"hello"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; RPUSH mylist <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line">redis 127.0.0.1:6379&gt; RPUSHX mylist2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis 127.0.0.1:6379&gt; LRANGE mylist 0 -1</div><div class="line">1) <span class="string">"hello"</span></div><div class="line">2) <span class="string">"foo"</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/2. Redis 配置文件/">Redis 配置文件</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<h2 id="1-redis查看配置"><a href="#1-redis查看配置" class="headerlink" title="1.redis查看配置"></a>1.redis查看配置</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># redis-cli</span></div><div class="line">127.0.0.1:6379&gt; CONFIG get *         <span class="comment"># 查看所有配置项</span></div><div class="line">127.0.0.1:6379&gt; CONFIG get timeout   <span class="comment"># 查看timeout</span></div><div class="line">1) <span class="string">"timeout"</span></div><div class="line">2) <span class="string">"300"</span></div><div class="line">127.0.0.1:6379&gt; CONFIG <span class="built_in">set</span> timeout 0 <span class="comment"># 重设 timeout</span></div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; CONFIG get timeout</div><div class="line">1) <span class="string">"timeout"</span></div><div class="line">2) <span class="string">"0"</span></div></pre></td></tr></table></figure>
<h2 id="2-redis-通用配置"><a href="#2-redis-通用配置" class="headerlink" title="2.redis 通用配置"></a>2.redis 通用配置</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">daemonize no                  <span class="comment"># 默认情况下，redis并不是以daemon形式来运行的。通过daemonize配置项可以控制redis的运行形式</span></div><div class="line">pidfile /path/to/redis.pid    <span class="comment"># 当以daemon形式运行时，redis会生成一个pid文件，默认会生成在/var/run/redis.pid</span></div><div class="line"><span class="built_in">bind</span> 192.168.1.2 10.8.4.2     <span class="comment"># 指定绑定的ip，可以有多个</span></div><div class="line">port 6379                     <span class="comment"># 指定监听端口</span></div><div class="line">unixsocket /tmp/redis.sock    <span class="comment"># 也可以监听socket</span></div><div class="line">unixsocketperm 755            <span class="comment"># 当监听socket时可以指定权限为755</span></div><div class="line">timeout 0                     <span class="comment"># 当一个redis-client一直没有请求发向server端，那么server端有权主   动关闭这个连接，可以通过timeout来设置“空闲超时时限”，0表示永不关闭。</span></div><div class="line">tcp-keepalive 0               <span class="comment"># TCP连接保活策略，可以通过tcp-keepalive配置项来进行设置，单位为秒，假如设置为60秒，则server端会每60秒向连接空闲的客户端发起一次ACK请求，以检查客户端是否已经挂掉，对于无响应的客户端则会关闭其连接。如果设置为0，则不会进行保活检测。</span></div><div class="line">loglevel notice               <span class="comment"># 日志级别，有四种debug, verbose, notice, warning</span></div><div class="line">logfile “”                    <span class="comment"># 定义日志路径，</span></div><div class="line">syslog-ident redis            <span class="comment"># 如果希望日志打印到syslog中，通过syslog-enabled来控制。另外，syslog-ident还可以让你指定syslog里的日志标志。</span></div><div class="line">syslog-facility local0        <span class="comment"># 指定syslog的设备，可以是USER或者local0-local7</span></div><div class="line">databases 16                  <span class="comment"># 设置数据库的总数量</span></div></pre></td></tr></table></figure>
<h2 id="3-redis-快照配置（rdb持久化）"><a href="#3-redis-快照配置（rdb持久化）" class="headerlink" title="3.redis 快照配置（rdb持久化）"></a>3.redis 快照配置（rdb持久化）</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">save 900 1                      <span class="comment"># 表示每15分钟且至少有1个key改变，就触发一次持久化 </span></div><div class="line">save 300 10                     <span class="comment"># 表示每5分钟且至少有10个key改变，就触发一次持久化</span></div><div class="line">save 60 10000                   <span class="comment"># 表示每60秒至少有10000个key改变，就触发一次持久</span></div><div class="line">save “”                         <span class="comment"># 这样可以禁用rdb持久化</span></div><div class="line">stop-writes-on-bgsave-error yes <span class="comment"># rdb持久化写入磁盘避免不了会出现失败的情况，默认一旦出现失败，redis会马上停止写操作。如果你觉得无所谓，那就可以使用该选项关闭这个功能。</span></div><div class="line">rdbcompression yes              <span class="comment"># 是否要压缩</span></div><div class="line">rdbchecksum yes                 <span class="comment"># 是否进行数据校验</span></div><div class="line">dbfilename dump.rdb             <span class="comment"># 定义快照文件的名字</span></div><div class="line">dir ./                          <span class="comment"># 定义快照文件储存路劲</span></div></pre></td></tr></table></figure>
<h2 id="4-redis-安全相关配置"><a href="#4-redis-安全相关配置" class="headerlink" title="4.redis 安全相关配置"></a>4.redis 安全相关配置</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">requirepass yanyi                      <span class="comment"># 设置redis-server的密码</span></div><div class="line">rename-command CONFIG  yanyi.config    <span class="comment"># 将CONFIG命令更名为yanyi.config，这样可以避免误操作，但如果使用了    AOF持久化，建议不要启用该功能</span></div><div class="line">rename-command CONFIG “”               <span class="comment"># 也可以后面定义为空，这样就禁掉了该CONFIG命令</span></div></pre></td></tr></table></figure>
<h2 id="5-redis-限制相关配置"><a href="#5-redis-限制相关配置" class="headerlink" title="5.redis 限制相关配置"></a>5.redis 限制相关配置</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">maxclients 10000              <span class="comment"># 限制最大客户端连接数</span></div><div class="line">maxmemory &lt;bytes&gt;             <span class="comment"># 设定最大内存使用数，单位是byte</span></div><div class="line">maxmemory-policy volatile-lru <span class="comment"># 指定内存移除规则</span></div><div class="line">maxmemory-samples 3           <span class="comment"># LRU算法和最小TTL算法都并非是精确的算法，而是估算值。所以你可以设置样本的大小。假如redis默认会检查三个key并选择其中LRU的那个，那么你可以改变这个key样本的数量。</span></div></pre></td></tr></table></figure>
<h2 id="6-redis-AOF-持久化相关配置"><a href="#6-redis-AOF-持久化相关配置" class="headerlink" title="6.redis AOF 持久化相关配置"></a>6.redis AOF 持久化相关配置</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">appendonly no                   <span class="comment"># 如果是no，则开启aof持久化</span></div><div class="line">appendfilename “appendonly.aof” <span class="comment"># 指定aof文件名字</span></div><div class="line">appendfsync everysec            <span class="comment"># 指定fsync()调用模式，有三种no(不调用fsync),always(每次写都会调用fsync),everysec(每秒钟调用一次fsync)。第一种最快，第二种数据最安全，但性能会差一些，第三种为这种方案，默认为第三种。</span></div><div class="line">no-appendfsync-on-rewrite no    <span class="comment"># 设置yes，可避免当写入量非常大时的磁盘io阻塞</span></div><div class="line">auto-aof-rewrite-percentage 10  <span class="comment"># 规定什么情况下会触发aof重写。该值为一个比例，10表示当aof文件增幅达到10%时则会触发重写机制。</span></div><div class="line">uto-aof-rewrite-min-size 64mb   <span class="comment"># 重写会有一个条件，就是不能低于64Mb</span></div></pre></td></tr></table></figure>
<h2 id="7-redis-慢日志相关配置"><a href="#7-redis-慢日志相关配置" class="headerlink" title="7.redis 慢日志相关配置"></a>7.redis 慢日志相关配置</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;针对慢日志，你可以设置两个参数，一个是执行时长，单位是微秒，另一个是慢日志的长度。当一个新的命令被写入日志时，最老的一条会从命令日志队列中被移除。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">slowlog-log-slower-than 10000  <span class="comment"># 慢于10000ms则记录日志</span></div><div class="line">slowlog-max-len 128            <span class="comment"># 日志长度</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/19. Redis 哈希(Hash)/">Redis 哈希(Hash)</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis hash 是一个string类型的field和value的映射表，hash特别适合用于存储对象。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis 中每个 hash 可以存储 232 - 1 键值对（40多亿）。</p>
<h2 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; HMSET runoobkey name <span class="string">"redis tutorial"</span> description <span class="string">"redis basic commands for caching"</span> likes 20 visitors 23000</div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; HGETALL runoobkey</div><div class="line">1) <span class="string">"name"</span></div><div class="line">2) <span class="string">"redis tutorial"</span></div><div class="line">3) <span class="string">"description"</span></div><div class="line">4) <span class="string">"redis basic commands for caching"</span></div><div class="line">5) <span class="string">"likes"</span></div><div class="line">6) <span class="string">"20"</span></div><div class="line">7) <span class="string">"visitors"</span></div><div class="line">8) <span class="string">"23000"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在以上实例中，我们设置了 redis 的一些描述信息(name, description, likes, visitors) 到哈希表的 <strong>runoobkey</strong> 中。</p>
<h2 id="Redis-hash-命令"><a href="#Redis-hash-命令" class="headerlink" title="Redis hash 命令"></a>Redis hash 命令</h2><h3 id="1-Redis-Hdel-命令"><a href="#1-Redis-Hdel-命令" class="headerlink" title="1.Redis Hdel 命令"></a>1.Redis Hdel 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HDEL key field2 [field2]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>删除一个或多个哈希表字段</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段，不存在的字段将被忽略。</p>
<h4 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hdel 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HDEL KEY_NAME FIELD1.. FIELDN</div></pre></td></tr></table></figure>
<h4 id="可用版本"><a href="#可用版本" class="headerlink" title="可用版本"></a>可用版本</h4><h4 id="返回值"><a href="#返回值" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;被成功删除字段的数量，不包括被忽略的字段。</p>
<h4 id="实例-1"><a href="#实例-1" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HDEL myhash field1</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HDEL myhash field2</div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="2-Redis-Hexists-命令"><a href="#2-Redis-Hexists-命令" class="headerlink" title="2.Redis Hexists 命令"></a>2.Redis Hexists 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HEXISTS key field</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<figure class="highlight plain"><figcaption><span>key 中，指定的字段是否存在```</span></figcaption><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;Redis Hexists 命令用于查看哈希表的指定字段是否存在。</div><div class="line"></div><div class="line">#### 语法</div><div class="line">&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;redis Hexists 命令基本语法如下：</div><div class="line"></div><div class="line">```bash</div><div class="line">redis 127.0.0.1:6379&gt; HEXISTS KEY_NAME FIELD_NAME</div></pre></td></tr></table></figure></p>
<h4 id="可用版本-1"><a href="#可用版本-1" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-1"><a href="#返回值-1" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果哈希表含有给定字段，返回 1 。 如果哈希表不含有给定字段，或 key 不存在，返回 0 。</p>
<h4 id="实例-2"><a href="#实例-2" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HEXISTS myhash field1</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HEXISTS myhash field2</div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="3-Redis-Hget-命令"><a href="#3-Redis-Hget-命令" class="headerlink" title="3.Redis Hget 命令"></a>3.Redis Hget 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HGET key field</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取存储在哈希表中指定字段的值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hget 命令用于返回哈希表中指定字段的值。</p>
<h4 id="语法-1"><a href="#语法-1" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hget 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HGET KEY_NAME FIELD_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-2"><a href="#可用版本-2" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-2"><a href="#返回值-2" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回给定字段的值。如果给定的字段或 key 不存在时，返回 nil 。</p>
<h4 id="实例-3"><a href="#实例-3" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 字段存在</span></div><div class="line"> </div><div class="line">redis&gt; HSET site redis redis.com</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis&gt; HGET site redis</div><div class="line"><span class="string">"redis.com"</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 字段不存在</span></div><div class="line"> </div><div class="line">redis&gt; HGET site mysql</div><div class="line">(nil)</div></pre></td></tr></table></figure>
<h3 id="4-Redis-Hgetall-命令"><a href="#4-Redis-Hgetall-命令" class="headerlink" title="4..Redis Hgetall 命令"></a>4..Redis Hgetall 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HGETALL key</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取在哈希表中指定 key 的所有字段和值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hgetall 命令用于返回哈希表中，所有的字段和值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在返回值里，紧跟每个字段名(field name)之后是字段的值(value)，所以返回值的长度是哈希表大小的两倍。</p>
<h4 id="语法-2"><a href="#语法-2" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hgetall 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HGETALL KEY_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-3"><a href="#可用版本-3" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-3"><a href="#返回值-3" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以列表形式返回哈希表的字段及字段值。 若 key 不存在，返回空列表。</p>
<h4 id="实例-4"><a href="#实例-4" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HGETALL myhash</div><div class="line">1) <span class="string">"field1"</span></div><div class="line">2) <span class="string">"Hello"</span></div><div class="line">3) <span class="string">"field2"</span></div><div class="line">4) <span class="string">"World"</span></div></pre></td></tr></table></figure>
<h3 id="5-Redis-Hincrby-命令"><a href="#5-Redis-Hincrby-命令" class="headerlink" title="5.Redis Hincrby 命令"></a>5.Redis Hincrby 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HINCRBY key field increment</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>为哈希表 key 中的指定字段的整数值加上增量 increment</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增量也可以为负数，相当于对指定字段进行减法操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果哈希表的 key 不存在，一个新的哈希表被创建并执行 HINCRBY 命令。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果指定的字段不存在，那么在执行命令前，字段的值被初始化为 0 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本操作的值被限制在 64 位(bit)有符号数字表示之内。</p>
<h4 id="语法-3"><a href="#语法-3" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hincrby 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HINCRBY KEY_NAME FIELD_NAME INCR_BY_NUMBER</div></pre></td></tr></table></figure>
<h4 id="可用版本-4"><a href="#可用版本-4" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-4"><a href="#返回值-4" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行 HINCRBY 命令之后，哈希表中字段的值。</p>
<h4 id="实例-5"><a href="#实例-5" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">redis&gt; HSET myhash field 5</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis&gt; HINCRBY myhash field 1</div><div class="line">(<span class="built_in">integer</span>) 6</div><div class="line">redis&gt; HINCRBY myhash field -1</div><div class="line">(<span class="built_in">integer</span>) 5</div><div class="line">redis&gt; HINCRBY myhash field -10</div><div class="line">(<span class="built_in">integer</span>) -5</div><div class="line">redis&gt;</div></pre></td></tr></table></figure>
<h3 id="6-Redis-Hincrbyfloat-命令"><a href="#6-Redis-Hincrbyfloat-命令" class="headerlink" title="6.Redis Hincrbyfloat 命令"></a>6.Redis Hincrbyfloat 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HINCRBYFLOAT key field increment</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>为哈希表 key 中的指定字段的浮点数值加上增量 increment</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果指定的字段不存在，那么在执行命令前，字段的值被初始化为 0 。</p>
<h4 id="语法-4"><a href="#语法-4" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hincrbyfloat 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HINCRBYFLOAT KEY_NAME FIELD_NAME INCR_BY_NUMBER</div></pre></td></tr></table></figure>
<h4 id="可用版本-5"><a href="#可用版本-5" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.6.0</p>
<h4 id="返回值-5"><a href="#返回值-5" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行 Hincrbyfloat 命令之后，哈希表中字段的值。</p>
<h4 id="实例-6"><a href="#实例-6" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">redis&gt; HSET mykey field 10.50</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis&gt; HINCRBYFLOAT mykey field 0.1</div><div class="line"><span class="string">"10.60000000000000001"</span></div><div class="line">redis&gt; HINCRBYFLOAT mykey field -5</div><div class="line"><span class="string">"5.59999999999999964"</span></div><div class="line">redis&gt; HSET mykey field 5.0e3</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis&gt; HINCRBYFLOAT mykey field 2.0e2</div><div class="line"><span class="string">"5200"</span></div><div class="line">redis&gt;</div></pre></td></tr></table></figure>
<h3 id="7-Redis-Hkeys-命令"><a href="#7-Redis-Hkeys-命令" class="headerlink" title="7.Redis Hkeys 命令"></a>7.Redis Hkeys 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HKEYS key</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取所有哈希表中的字段</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hkeys 命令用于获取哈希表中的所有字段名。</p>
<h4 id="语法-5"><a href="#语法-5" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hkeys 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HKEYS KEY_NAME FIELD_NAME INCR_BY_NUMBER</div></pre></td></tr></table></figure>
<h4 id="可用版本-6"><a href="#可用版本-6" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-6"><a href="#返回值-6" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;包含哈希表中所有字段的列表。 当 key 不存在时，返回一个空列表。</p>
<h4 id="实例-7"><a href="#实例-7" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HKEYS myhash</div><div class="line">1) <span class="string">"field1"</span></div><div class="line">2) <span class="string">"field2"</span></div></pre></td></tr></table></figure>
<h3 id="8-Redis-Hlen-命令"><a href="#8-Redis-Hlen-命令" class="headerlink" title="8.Redis Hlen 命令"></a>8.Redis Hlen 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HLEN key</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取哈希表中字段的数量</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hlen 命令用于获取哈希表中字段的数量。</p>
<h4 id="语法-6"><a href="#语法-6" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hlen 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HLEN KEY_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-7"><a href="#可用版本-7" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-7"><a href="#返回值-7" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;哈希表中字段的数量。 当 key 不存在时，返回 0 。</p>
<h4 id="实例-8"><a href="#实例-8" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HLEN myhash</div><div class="line">(<span class="built_in">integer</span>) 2</div></pre></td></tr></table></figure>
<h3 id="9-Redis-Hmget-命令"><a href="#9-Redis-Hmget-命令" class="headerlink" title="9.Redis Hmget 命令"></a>9.Redis Hmget 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HMGET key field1 [field2]</code><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取所有给定字段的值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hmget 命令用于返回哈希表中，一个或多个给定字段的值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果指定的字段不存在于哈希表，那么返回一个 nil 值。</p>
<h4 id="语法-7"><a href="#语法-7" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hmget 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HMGET KEY_NAME FIELD1...FIELDN</div></pre></td></tr></table></figure>
<h4 id="可用版本-8"><a href="#可用版本-8" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-8"><a href="#返回值-8" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个包含多个给定字段关联值的表，表值的排列顺序和指定字段的请求顺序一样。</p>
<h4 id="实例-9"><a href="#实例-9" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HMGET myhash field1 field2 nofield</div><div class="line">1) <span class="string">"foo"</span></div><div class="line">2) <span class="string">"bar"</span></div><div class="line">3) (nil)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HMSET key field1 value1 [field2 value2 ]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>同时将多个 field-value (域-值)对设置到哈希表 key 中</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hmset 命令用于同时将多个 field-value (字段-值)对设置到哈希表中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;此命令会覆盖哈希表中已存在的字段。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果哈希表不存在，会创建一个空哈希表，并执行 HMSET 操作。</p>
<h4 id="语法-8"><a href="#语法-8" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hmset 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HMSET KEY_NAME FIELD1 VALUE1 ...FIELDN VALUEN</div></pre></td></tr></table></figure>
<h4 id="可用版本-9"><a href="#可用版本-9" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-9"><a href="#返回值-9" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果命令执行成功，返回 OK 。</p>
<h4 id="实例-10"><a href="#实例-10" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HMSET myhash field1 <span class="string">"Hello"</span> field2 <span class="string">"World"</span></div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; HGET myhash field1</div><div class="line"><span class="string">"Hello"</span></div><div class="line">redis 127.0.0.1:6379&gt; HGET myhash field2</div><div class="line"><span class="string">"World"</span></div></pre></td></tr></table></figure>
<h3 id="11-Redis-Hset-命令"><a href="#11-Redis-Hset-命令" class="headerlink" title="11.Redis Hset 命令"></a>11.Redis Hset 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HSET key field value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>将哈希表 key 中的字段 field 的值设为 value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hset 命令用于为哈希表中的字段赋值 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果哈希表不存在，一个新的哈希表被创建并进行 HSET 操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果字段已经存在于哈希表中，旧值将被覆盖。</p>
<h4 id="语法-9"><a href="#语法-9" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hset 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET KEY_NAME FIELD VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-10"><a href="#可用版本-10" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-10"><a href="#返回值-10" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果字段是哈希表中的一个新建字段，并且值设置成功，返回 1 。 如果哈希表中域字段已经存在且旧值已被新值覆盖，返回 0 。</p>
<h4 id="实例-11"><a href="#实例-11" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; HGET myhash field1</div><div class="line"><span class="string">"foo"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; HSET website google <span class="string">"www.g.cn"</span> <span class="comment"># 设置一个新域</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt;HSET website google <span class="string">"www.google.com"</span> <span class="comment"># 覆盖一个旧域</span></div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="12-Redis-Hsetnx-命令"><a href="#12-Redis-Hsetnx-命令" class="headerlink" title="12.Redis Hsetnx 命令"></a>12.Redis Hsetnx 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HSETNX key field value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>只有在字段 field 不存在时，设置哈希表字段的值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果哈希表不存在，一个新的哈希表被创建并进行 HSET 操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果字段已经存在于哈希表中，操作无效。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果 key 不存在，一个新哈希表被创建并执行 HSETNX 命令。</p>
<h4 id="语法-10"><a href="#语法-10" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hsetnx 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSETNX KEY_NAME FIELD VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-11"><a href="#可用版本-11" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-11"><a href="#返回值-11" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置成功，返回 1 。 如果给定字段已经存在且没有操作被执行，返回 0 。</p>
<h4 id="实例-12"><a href="#实例-12" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSETNX myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HSETNX myhash field1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis 127.0.0.1:6379&gt; HGET myhash field1</div><div class="line"><span class="string">"foo"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; HSETNX nosql key-value-store redis</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; HSETNX nosql key-value-store redis <span class="comment"># 操作无效， key-value-store 已存在</span></div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="13-Redis-Hvals-命令"><a href="#13-Redis-Hvals-命令" class="headerlink" title="13.Redis Hvals 命令"></a>13.Redis Hvals 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HVALS key</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取哈希表中所有值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hvals 命令返回哈希表所有字段的值。</p>
<h4 id="语法-11"><a href="#语法-11" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hvals 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HVALS KEY_NAME FIELD VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-12"><a href="#可用版本-12" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-12"><a href="#返回值-12" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个包含哈希表中所有值的表。 当 key 不存在时，返回一个空表。</p>
<h4 id="实例-13"><a href="#实例-13" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HVALS myhash</div><div class="line">1) <span class="string">"foo"</span></div><div class="line">2) <span class="string">"bar"</span></div><div class="line"> </div><div class="line"><span class="comment"># 空哈希表/不存在的key</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; EXISTS not_exists</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; HVALS not_exists</div><div class="line">(empty list or <span class="built_in">set</span>)</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/18. Redis 字符串(String)/">Redis 字符串(String)</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis 字符串数据类型的相关命令用于管理 redis 字符串值，基本语法如下：</p>
<h2 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; COMMAND KEY_NAME</div></pre></td></tr></table></figure>
<h2 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SET runoobkey redis</div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; GET runoobkey</div><div class="line"><span class="string">"redis"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在以上实例中我们使用了 <strong>SET</strong> 和 <strong>GET</strong> 命令，键为 <strong>runoobkey</strong>。</p>
<h2 id="Redis-字符串命令"><a href="#Redis-字符串命令" class="headerlink" title="Redis 字符串命令"></a>Redis 字符串命令</h2><h3 id="1-Redis-SET-命令"><a href="#1-Redis-SET-命令" class="headerlink" title="1.Redis SET 命令"></a>1.Redis SET 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SET key value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>设置指定 key 的值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis SET 命令用于设置给定 key 的值。如果 key 已经存储其他值， SET 就覆写旧值，且无视类型。</p>
<h4 id="语法-1"><a href="#语法-1" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis SET 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SET KEY_NAME VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本"><a href="#可用版本" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值"><a href="#返回值" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 Redis 2.6.12 以前版本， SET 命令总是返回 OK 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从 Redis 2.6.12 版本开始， SET 在设置操作成功完成时，才返回 OK 。</p>
<h4 id="实例-1"><a href="#实例-1" class="headerlink" title="实例"></a>实例</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，我们在 redis 中创建一个 key 并设置值。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 对不存在的键进行设置</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; SET key <span class="string">"value"</span></div><div class="line">OK</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; GET key</div><div class="line"><span class="string">"value"</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 对已存在的键进行设置</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; SET key <span class="string">"new-value"</span></div><div class="line">OK</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; GET key</div><div class="line"><span class="string">"new-value"</span></div></pre></td></tr></table></figure>
<h3 id="2-Redis-Get-命令"><a href="#2-Redis-Get-命令" class="headerlink" title="2.Redis Get 命令"></a>2.Redis Get 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>GET key</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取指定 key 的值。</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Get 命令用于获取指定 key 的值。如果 key 不存在，返回 nil 。如果key 储存的值不是字符串类型，返回一个错误。</p>
<h4 id="语法-2"><a href="#语法-2" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Get 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; GET KEY_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-1"><a href="#可用版本-1" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-1"><a href="#返回值-1" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回 key 的值，如果 key 不存在时，返回 nil。 如果 key 不是字符串类型，那么返回一个错误。</p>
<h4 id="实例-2"><a href="#实例-2" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 对不存在的 key 或字符串类型 key 进行 GET</span></div><div class="line"> </div><div class="line">redis&gt; GET db</div><div class="line">(nil)</div><div class="line"> </div><div class="line">redis&gt; SET db redis</div><div class="line">OK</div><div class="line"> </div><div class="line">redis&gt; GET db</div><div class="line"><span class="string">"redis"</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 对不是字符串类型的 key 进行 GET</span></div><div class="line"> </div><div class="line">redis&gt; DEL db</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis&gt; LPUSH db redis mongodb mysql</div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line"> </div><div class="line">redis&gt; GET db</div><div class="line">(error) ERR Operation against a key holding the wrong kind of value</div></pre></td></tr></table></figure>
<h3 id="3-Redis-Getrange-命令"><a href="#3-Redis-Getrange-命令" class="headerlink" title="3.Redis Getrange 命令"></a>3.Redis Getrange 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>GETRANGE key start end</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>返回 key 中字符串值的子字符</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Getrange 命令用于获取存储在指定 key 中字符串的子字符串。字符串的截取范围由 start 和 end 两个偏移量决定(包括 start 和 end 在内)。</p>
<h4 id="语法-3"><a href="#语法-3" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Getrange 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; GETRANGE KEY_NAME start end</div></pre></td></tr></table></figure>
<h4 id="可用版本-2"><a href="#可用版本-2" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.4.0</p>
<h4 id="返回值-2"><a href="#返回值-2" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;截取得到的子字符串。</p>
<h4 id="实例-3"><a href="#实例-3" class="headerlink" title="实例"></a>实例</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，设置 mykey 的值并截取字符串。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SET mykey <span class="string">"This is my test key"</span></div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; GETRANGE mykey 0 3</div><div class="line"><span class="string">"This"</span></div><div class="line">redis 127.0.0.1:6379&gt; GETRANGE mykey 0 -1</div><div class="line"><span class="string">"This is my test key"</span></div></pre></td></tr></table></figure>
<h3 id="4-Redis-Getset-命令"><a href="#4-Redis-Getset-命令" class="headerlink" title="4.Redis Getset 命令"></a>4.Redis Getset 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>GETSET key value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>将给定 key 的值设为 value ，并返回 key 的旧值(old value)。</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Getset 命令用于设置指定 key 的值，并返回 key 旧的值。</p>
<h4 id="语法-4"><a href="#语法-4" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Getset 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; GETSET KEY_NAME VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-3"><a href="#可用版本-3" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-3"><a href="#返回值-3" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回给定 key 的旧值。 当 key 没有旧值时，即 key 不存在时，返回 nil 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当 key 存在但不是字符串类型时，返回一个错误。</p>
<h4 id="实例-4"><a href="#实例-4" class="headerlink" title="实例"></a>实例</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，设置 mykey 的值并截取字符串。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; GETSET mynewkey <span class="string">"This is my test key"</span></div><div class="line">(nil)</div><div class="line">redis 127.0.0.1:6379&gt; GETSET mynewkey <span class="string">"This is my new value to test getset"</span></div><div class="line"><span class="string">"This is my test key"</span></div></pre></td></tr></table></figure>
<h3 id="5-Redis-Getbit-命令"><a href="#5-Redis-Getbit-命令" class="headerlink" title="5.Redis Getbit 命令"></a>5.Redis Getbit 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>GETBIT key offset</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>对 key 所储存的字符串值，获取指定偏移量上的位(bit)。</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Getbit 命令用于对 key 所储存的字符串值，获取指定偏移量上的位(bit)。</p>
<h4 id="语法-5"><a href="#语法-5" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Getbit 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; GETBIT KEY_NAME OFFSET</div></pre></td></tr></table></figure>
<h4 id="可用版本-4"><a href="#可用版本-4" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.2.0</p>
<h4 id="返回值-4"><a href="#返回值-4" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;字符串值指定偏移量上的位(bit)。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当偏移量 OFFSET 比字符串值的长度大，或者 key 不存在时，返回 0 。</p>
<h4 id="实例-5"><a href="#实例-5" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 对不存在的 key 或者不存在的 offset 进行 GETBIT， 返回 0</span></div><div class="line"> </div><div class="line">redis&gt; EXISTS bit</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line">redis&gt; GETBIT bit 10086</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 对已存在的 offset 进行 GETBIT</span></div><div class="line"> </div><div class="line">redis&gt; SETBIT bit 10086 1</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line">redis&gt; GETBIT bit 10086</div><div class="line">(<span class="built_in">integer</span>) 1</div></pre></td></tr></table></figure>
<h3 id="6-Redis-Mget-命令"><a href="#6-Redis-Mget-命令" class="headerlink" title="6.Redis Mget 命令"></a>6.Redis Mget 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>MGET key1 [key2..]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取所有(一个或多个)给定 key 的值。</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Mget 命令返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面，有某个 key 不存在，那么这个 key 返回特殊值 nil 。</p>
<h4 id="语法-6"><a href="#语法-6" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Mget 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; MGET KEY1 KEY2 .. KEYN</div></pre></td></tr></table></figure>
<h4 id="可用版本-5"><a href="#可用版本-5" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-5"><a href="#返回值-5" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个包含所有给定 key 的值的列表。</p>
<h4 id="实例-6"><a href="#实例-6" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SET key1 <span class="string">"hello"</span></div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; SET key2 <span class="string">"world"</span></div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; MGET key1 key2 someOtherKey</div><div class="line">1) <span class="string">"Hello"</span></div><div class="line">2) <span class="string">"World"</span></div><div class="line">3) (nil)</div></pre></td></tr></table></figure>
<h3 id="7-Redis-Setbit-命令"><a href="#7-Redis-Setbit-命令" class="headerlink" title="7.Redis Setbit 命令"></a>7.Redis Setbit 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SETBIT key offset value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>对 key 所储存的字符串值，设置或清除指定偏移量上的位(bit)。</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Setbit 命令用于对 key 所储存的字符串值，设置或清除指定偏移量上的位(bit)。</p>
<h4 id="语法-7"><a href="#语法-7" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Setbit 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; Setbit KEY_NAME OFFSET</div></pre></td></tr></table></figure>
<h4 id="可用版本-6"><a href="#可用版本-6" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.2.0</p>
<h4 id="返回值-6"><a href="#返回值-6" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定偏移量原来储存的位。</p>
<h4 id="实例-7"><a href="#实例-7" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">redis&gt; SETBIT bit 10086 1</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line">redis&gt; GETBIT bit 10086</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis&gt; GETBIT bit 100 <span class="comment"># bit 默认被初始化为 0</span></div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="8-Redis-Setex-命令"><a href="#8-Redis-Setex-命令" class="headerlink" title="8.Redis Setex 命令"></a>8.Redis Setex 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SETEX key seconds value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>将值 value 关联到 key ，并将 key 的过期时间设为 seconds (以秒为单位)。</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Setex 命令为指定的 key 设置值及其过期时间。如果 key 已经存在， SETEX 命令将会替换旧的值。</p>
<h4 id="语法-8"><a href="#语法-8" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Setex 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SETEX KEY_NAME TIMEOUT VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-7"><a href="#可用版本-7" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-7"><a href="#返回值-7" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置成功时返回 OK 。</p>
<h4 id="实例-8"><a href="#实例-8" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SETEX mykey 60 redis</div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; TTL mykey</div><div class="line">60</div><div class="line">redis 127.0.0.1:6379&gt; GET mykey</div><div class="line"><span class="string">"redis</span></div></pre></td></tr></table></figure>
<h3 id="9-Redis-Setnx-命令"><a href="#9-Redis-Setnx-命令" class="headerlink" title="9.Redis Setnx 命令"></a>9.Redis Setnx 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SETNX key value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>只有在 key 不存在时设置 key 的值。</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Setnx（SET if Not eXists） 命令在指定的 key 不存在时，为 key 设置指定的值。</p>
<h4 id="语法-9"><a href="#语法-9" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Setnx 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SETNX KEY_NAME VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-8"><a href="#可用版本-8" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-8"><a href="#返回值-8" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置成功，返回 1 。 设置失败，返回 0 。</p>
<h4 id="实例-9"><a href="#实例-9" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">redis&gt; EXISTS job <span class="comment"># job 不存在</span></div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line">redis&gt; SETNX job <span class="string">"programmer"</span> <span class="comment"># job 设置成功</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis&gt; SETNX job <span class="string">"code-farmer"</span> <span class="comment"># 尝试覆盖 job ，失败</span></div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line">redis&gt; GET job <span class="comment"># 没有被覆盖</span></div><div class="line"><span class="string">"programmer"</span></div></pre></td></tr></table></figure>
<h3 id="10-Redis-Setrange-命令"><a href="#10-Redis-Setrange-命令" class="headerlink" title="10.Redis Setrange 命令"></a>10.Redis Setrange 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>SETRANGE key offset value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>用 value 参数覆写给定 key 所储存的字符串值，从偏移量 offset 开始。</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;edis Setrange 命令用指定的字符串覆盖给定 key 所储存的字符串值，覆盖的位置从偏移量 offset 开始。</p>
<h4 id="语法-10"><a href="#语法-10" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Setrange 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SETRANGE KEY_NAME OFFSET VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-9"><a href="#可用版本-9" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.2.0</p>
<h4 id="返回值-9"><a href="#返回值-9" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;被修改后的字符串长度。</p>
<h4 id="实例-10"><a href="#实例-10" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; SET key1 <span class="string">"Hello World"</span></div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; SETRANGE key1 6 <span class="string">"Redis"</span></div><div class="line">(<span class="built_in">integer</span>) 11</div><div class="line">redis 127.0.0.1:6379&gt; GET key1</div><div class="line"><span class="string">"Hello Redis"</span></div></pre></td></tr></table></figure>
<h3 id="11-Redis-Strlen-命令"><a href="#11-Redis-Strlen-命令" class="headerlink" title="11.Redis Strlen 命令"></a>11.Redis Strlen 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>STRLEN key</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>返回 key 所储存的字符串值的长度。</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Strlen 命令用于获取指定 key 所储存的字符串值的长度。当 key 储存的不是字符串值时，返回一个错误。</p>
<h4 id="语法-11"><a href="#语法-11" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Strlen 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; STRLEN KEY_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-10"><a href="#可用版本-10" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.2.0</p>
<h4 id="返回值-10"><a href="#返回值-10" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;字符串值的长度。 当 key 不存在时，返回 0。</p>
<h4 id="实例-11"><a href="#实例-11" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 获取字符串的长度</span></div><div class="line"> </div><div class="line">redis&gt; SET mykey <span class="string">"Hello world"</span></div><div class="line">OK</div><div class="line"> </div><div class="line">redis&gt; STRLEN mykey</div><div class="line">(<span class="built_in">integer</span>) 11</div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 不存在的 key 长度为 0</span></div><div class="line"> </div><div class="line">redis&gt; STRLEN nonexisting</div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="12-Redis-Mset-命令"><a href="#12-Redis-Mset-命令" class="headerlink" title="12.Redis Mset 命令"></a>12.Redis Mset 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>MSET key value [key value ...]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>同时设置一个或多个 key-value 对</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Mset 命令用于同时设置一个或多个 key-value 对。</p>
<h4 id="语法-12"><a href="#语法-12" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Mset 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; MSET key1 value1 key2 value2 .. keyN valueN</div></pre></td></tr></table></figure>
<h4 id="可用版本-11"><a href="#可用版本-11" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.1</p>
<h4 id="返回值-11"><a href="#返回值-11" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;总是返回 OK 。</p>
<h4 id="实例-12"><a href="#实例-12" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; MSET key1 <span class="string">"Hello"</span> key2 <span class="string">"World"</span></div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; GET key1</div><div class="line"><span class="string">"Hello"</span></div><div class="line">redis 127.0.0.1:6379&gt; GET key2</div><div class="line">1) <span class="string">"World"</span></div></pre></td></tr></table></figure>
<h3 id="13-Redis-Msetnx-命令"><a href="#13-Redis-Msetnx-命令" class="headerlink" title="13.Redis Msetnx 命令"></a>13.Redis Msetnx 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>MSETNX key value [key value ...]</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>同时设置一个或多个 key-value 对，当且仅当所有给定 key 都不存在。</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Msetnx 命令用于所有给定 key 都不存在时，同时设置一个或多个 key-value 对。</p>
<h4 id="语法-13"><a href="#语法-13" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Msetnx 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; MSETNX key1 value1 key2 value2 .. keyN valueN</div></pre></td></tr></table></figure>
<h4 id="可用版本-12"><a href="#可用版本-12" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.1</p>
<h4 id="返回值-12"><a href="#返回值-12" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当所有 key 都成功设置，返回 1 。 如果所有给定 key 都设置失败(至少有一个 key 已经存在)，那么返回 0 。</p>
<h4 id="实例-13"><a href="#实例-13" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 对不存在的 key 进行 MSETNX</span></div><div class="line"> </div><div class="line">redis&gt; MSETNX rmdbs <span class="string">"MySQL"</span> nosql <span class="string">"MongoDB"</span> key-value-store <span class="string">"redis"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis&gt; MGET rmdbs nosql key-value-store</div><div class="line">1) <span class="string">"MySQL"</span></div><div class="line">2) <span class="string">"MongoDB"</span></div><div class="line">3) <span class="string">"redis"</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># MSET 的给定 key 当中有已存在的 key</span></div><div class="line"> </div><div class="line">redis&gt; MSETNX rmdbs <span class="string">"Sqlite"</span> language <span class="string">"python"</span> <span class="comment"># rmdbs 键已经存在，操作失败</span></div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line">redis&gt; EXISTS language <span class="comment"># 因为 MSET 是原子性操作，language 没有被设置</span></div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line">redis&gt; GET rmdbs <span class="comment"># rmdbs 也没有被修改</span></div><div class="line"><span class="string">"MySQL"</span></div></pre></td></tr></table></figure>
<h3 id="14-Redis-Psetex-命令"><a href="#14-Redis-Psetex-命令" class="headerlink" title="14.Redis Psetex 命令"></a>14.Redis Psetex 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>PSETEX key milliseconds value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>这个命令和 SETEX 命令相似，但它以毫秒为单位设置 key 的生存时间，而不是像 SETEX 命令那样，以秒为单位</code>  </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Psetex 命令以毫秒为单位设置 key 的生存时间。</p>
<h4 id="语法-14"><a href="#语法-14" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Psetex 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; PSETEX key1 EXPIRY_IN_MILLISECONDS value1</div></pre></td></tr></table></figure>
<h4 id="可用版本-13"><a href="#可用版本-13" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.6.0</p>
<h4 id="返回值-13"><a href="#返回值-13" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置成功时返回 OK 。</p>
<h4 id="实例-14"><a href="#实例-14" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; PSETEX mykey 1000 <span class="string">"Hello"</span></div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; PTTL mykey</div><div class="line">999</div><div class="line">redis 127.0.0.1:6379&gt; GET mykey</div><div class="line">1) <span class="string">"Hello"</span></div></pre></td></tr></table></figure>
<h3 id="15-Redis-Incr-命令"><a href="#15-Redis-Incr-命令" class="headerlink" title="15.Redis Incr 命令"></a>15.Redis Incr 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>INCR key</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>将 key 中储存的数字值增一。</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Incr 命令将 key 中储存的数字值增一。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果 key 不存在，那么 key 的值会先被初始化为 0 ，然后再执行 INCR 操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果值包含错误的类型，或字符串类型的值不能表示为数字，那么返回一个错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本操作的值限制在 64 位(bit)有符号数字表示之内。</p>
<h4 id="语法-15"><a href="#语法-15" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Incr 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; INCR KEY_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-14"><a href="#可用版本-14" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-14"><a href="#返回值-14" class="headerlink" title="返回值"></a>返回值</h4><h4 id="实例-15"><a href="#实例-15" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">redis&gt; SET page_view 20</div><div class="line">OK</div><div class="line"> </div><div class="line">redis&gt; INCR page_view</div><div class="line">(<span class="built_in">integer</span>) 21</div><div class="line"> </div><div class="line">redis&gt; GET page_view <span class="comment"># 数字值在 Redis 中以字符串的形式保存</span></div><div class="line"><span class="string">"21"</span></div></pre></td></tr></table></figure>
<h3 id="16-Redis-Incrby-命令"><a href="#16-Redis-Incrby-命令" class="headerlink" title="16.Redis Incrby 命令"></a>16.Redis Incrby 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>INCRBY key increment</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>将 key 所储存的值加上给定的增量值（increment）</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Incrby 命令将 key 中储存的数字加上指定的增量值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果 key 不存在，那么 key 的值会先被初始化为 0 ，然后再执行 INCRBY 命令。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果值包含错误的类型，或字符串类型的值不能表示为数字，那么返回一个错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本操作的值限制在 64 位(bit)有符号数字表示之内。</p>
<h4 id="语法-16"><a href="#语法-16" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Incrby 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; INCRBY KEY_NAME INCR_AMOUNT</div></pre></td></tr></table></figure>
<h4 id="可用版本-15"><a href="#可用版本-15" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-15"><a href="#返回值-15" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加上指定的增量值之后， key 的值。</p>
<h4 id="实例-16"><a href="#实例-16" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># key 存在且是数字值</span></div><div class="line"> </div><div class="line">redis&gt; SET rank 50</div><div class="line">OK</div><div class="line"> </div><div class="line">redis&gt; INCRBY rank 20</div><div class="line">(<span class="built_in">integer</span>) 70</div><div class="line"> </div><div class="line">redis&gt; GET rank</div><div class="line"><span class="string">"70"</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># key 不存在时</span></div><div class="line"> </div><div class="line">redis&gt; EXISTS counter</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line">redis&gt; INCRBY counter 30</div><div class="line">(<span class="built_in">integer</span>) 30</div><div class="line"> </div><div class="line">redis&gt; GET counter</div><div class="line"><span class="string">"30"</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># key 不是数字值时</span></div><div class="line"> </div><div class="line">redis&gt; SET book <span class="string">"long long ago..."</span></div><div class="line">OK</div><div class="line"> </div><div class="line">redis&gt; INCRBY book 200</div><div class="line">(error) ERR value is not an <span class="built_in">integer</span> or out of range</div></pre></td></tr></table></figure>
<h3 id="17-Redis-Incrbyfloat-命令"><a href="#17-Redis-Incrbyfloat-命令" class="headerlink" title="17.Redis Incrbyfloat 命令"></a>17.Redis Incrbyfloat 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>INCRBYFLOAT key increment</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>将 key 所储存的值加上给定的浮点增量值（increment）</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果 key 不存在，那么 INCRBYFLOAT 会先将 key 的值设为 0 ，再执行加法操作。</p>
<h4 id="语法-17"><a href="#语法-17" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Incrbyfloat 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; INCRBYFLOAT KEY_NAME INCR_AMOUNT</div></pre></td></tr></table></figure>
<h4 id="可用版本-16"><a href="#可用版本-16" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.6.0</p>
<h4 id="返回值-16"><a href="#返回值-16" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行命令之后 key 的值。</p>
<h4 id="实例-17"><a href="#实例-17" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 值和增量都不是指数符号</span></div><div class="line"> </div><div class="line">redis&gt; SET mykey 10.50</div><div class="line">OK</div><div class="line"> </div><div class="line">redis&gt; INCRBYFLOAT mykey 0.1</div><div class="line"><span class="string">"10.6"</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 值和增量都是指数符号</span></div><div class="line"> </div><div class="line">redis&gt; SET mykey 314e-2</div><div class="line">OK</div><div class="line"> </div><div class="line">redis&gt; GET mykey <span class="comment"># 用 SET 设置的值可以是指数符号</span></div><div class="line"><span class="string">"314e-2"</span></div><div class="line"> </div><div class="line">redis&gt; INCRBYFLOAT mykey 0 <span class="comment"># 但执行 INCRBYFLOAT 之后格式会被改成非指数符号</span></div><div class="line"><span class="string">"3.14"</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 可以对整数类型执行</span></div><div class="line"> </div><div class="line">redis&gt; SET mykey 3</div><div class="line">OK</div><div class="line"> </div><div class="line">redis&gt; INCRBYFLOAT mykey 1.1</div><div class="line"><span class="string">"4.1"</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 后跟的 0 会被移除</span></div><div class="line"> </div><div class="line">redis&gt; SET mykey 3.0</div><div class="line">OK</div><div class="line"> </div><div class="line">redis&gt; GET mykey <span class="comment"># SET 设置的值小数部分可以是 0</span></div><div class="line"><span class="string">"3.0"</span></div><div class="line"> </div><div class="line">redis&gt; INCRBYFLOAT mykey 1.000000000000000000000 <span class="comment"># 但 INCRBYFLOAT 会将无用的 0 忽略掉，有需要的话，将浮点变为整数</span></div><div class="line"><span class="string">"4"</span></div><div class="line"> </div><div class="line">redis&gt; GET mykey</div><div class="line"><span class="string">"4"</span></div></pre></td></tr></table></figure>
<h3 id="18-Redis-Decr-命令"><a href="#18-Redis-Decr-命令" class="headerlink" title="18.Redis Decr 命令"></a>18.Redis Decr 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>DECR key</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>将 key 中储存的数字值减一</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Decr 命令将 key 中储存的数字值减一。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果 key 不存在，那么 key 的值会先被初始化为 0 ，然后再执行 DECR 操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果值包含错误的类型，或字符串类型的值不能表示为数字，那么返回一个错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本操作的值限制在 64 位(bit)有符号数字表示之内。</p>
<h4 id="语法-18"><a href="#语法-18" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Decr 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; DECR KEY_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-17"><a href="#可用版本-17" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-17"><a href="#返回值-17" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行命令之后 key 的值。</p>
<h4 id="实例-18"><a href="#实例-18" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 对存在的数字值 key 进行 DECR</span></div><div class="line"> </div><div class="line">redis&gt; SET failure_times 10</div><div class="line">OK</div><div class="line"> </div><div class="line">redis&gt; DECR failure_times</div><div class="line">(<span class="built_in">integer</span>) 9</div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 对不存在的 key 值进行 DECR</span></div><div class="line"> </div><div class="line">redis&gt; EXISTS count</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line">redis&gt; DECR count</div><div class="line">(<span class="built_in">integer</span>) -1</div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 对存在但不是数值的 key 进行 DECR</span></div><div class="line"> </div><div class="line">redis&gt; SET company YOUR_CODE_SUCKS.LLC</div><div class="line">OK</div><div class="line"> </div><div class="line">redis&gt; DECR company</div><div class="line">(error) ERR value is not an <span class="built_in">integer</span> or out of range</div></pre></td></tr></table></figure>
<h3 id="19-Redis-Decrby-命令"><a href="#19-Redis-Decrby-命令" class="headerlink" title="19.Redis Decrby 命令"></a>19.Redis Decrby 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>DECRBY key decrement</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>key 所储存的值减去给定的减量值（decrement）</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Decrby 命令将 key 所储存的值减去指定的减量值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果 key 不存在，那么 key 的值会先被初始化为 0 ，然后再执行 DECRBY 操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果值包含错误的类型，或字符串类型的值不能表示为数字，那么返回一个错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本操作的值限制在 64 位(bit)有符号数字表示之内。</p>
<h4 id="语法-19"><a href="#语法-19" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Decrby 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; DECRBY KEY_NAME DECREMENT_AMOUNT</div></pre></td></tr></table></figure>
<h4 id="可用版本-18"><a href="#可用版本-18" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 1.0.0</p>
<h4 id="返回值-18"><a href="#返回值-18" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;减去指定减量值之后， key 的值。</p>
<h4 id="实例-19"><a href="#实例-19" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 对已存在的 key 进行 DECRBY</span></div><div class="line"> </div><div class="line">redis&gt; SET count 100</div><div class="line">OK</div><div class="line"> </div><div class="line">redis&gt; DECRBY count 20</div><div class="line">(<span class="built_in">integer</span>) 80</div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 对不存在的 key 进行DECRBY</span></div><div class="line"> </div><div class="line">redis&gt; EXISTS pages</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line">redis&gt; DECRBY pages 10</div><div class="line">(<span class="built_in">integer</span>) -10</div></pre></td></tr></table></figure>
<h3 id="20-Redis-Append-命令"><a href="#20-Redis-Append-命令" class="headerlink" title="20.Redis Append 命令"></a>20.Redis Append 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>APPEND key value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>如果 key 已经存在并且是一个字符串， APPEND 命令将 value 追加到 key 原来的值的末尾</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Append 命令用于为指定的 key 追加值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果 key 已经存在并且是一个字符串， APPEND 命令将 value 追加到 key 原来的值的末尾。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果 key 不存在， APPEND 就简单地将给定 key 设为 value ，就像执行 SET key value 一样。</p>
<h4 id="语法-20"><a href="#语法-20" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Append 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; APPEND KEY_NAME NEW_VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-19"><a href="#可用版本-19" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-19"><a href="#返回值-19" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;追加指定值之后， key 中字符串的长度。</p>
<h4 id="实例-20"><a href="#实例-20" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 对不存在的 key 执行 APPEND</span></div><div class="line"> </div><div class="line">redis&gt; EXISTS myphone <span class="comment"># 确保 myphone 不存在</span></div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line">redis&gt; APPEND myphone <span class="string">"nokia"</span> <span class="comment"># 对不存在的 key 进行 APPEND ，等同于 SET myphone "nokia"</span></div><div class="line">(<span class="built_in">integer</span>) 5 <span class="comment"># 字符长度</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 对已存在的字符串进行 APPEND</span></div><div class="line"> </div><div class="line">redis&gt; APPEND myphone <span class="string">" - 1110"</span> <span class="comment"># 长度从 5 个字符增加到 12 个字符</span></div><div class="line">(<span class="built_in">integer</span>) 12</div><div class="line"> </div><div class="line">redis&gt; GET myphone</div><div class="line"><span class="string">"nokia - 1110"</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/17. Redis 命令/">Redis 命令</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis 命令用于在 redis 服务上执行操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;要在 redis 服务上执行命令需要一个 redis 客户端。Redis 客户端在我们之前下载 的 redis 的安装包中。</p>
<h2 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis 客户端的基本语法为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis-cli</div></pre></td></tr></table></figure>
<h2 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下实例讲解了如何启动 redis 客户端：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动 redis 客户端，打开终端并输入命令 <strong>redis-cli</strong>。该命令会连接本地的 redis 服务。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">redis-cli</div><div class="line">redis 127.0.0.1:6379&gt;</div><div class="line">redis 127.0.0.1:6379&gt; PING</div><div class="line"> </div><div class="line">PONG</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在以上实例中我们连接到本地的 redis 服务并执行 <strong>PING</strong> 命令，该命令用于检测 redis 服务是否启动。</p>
<h2 id="在远程服务上执行命令"><a href="#在远程服务上执行命令" class="headerlink" title="在远程服务上执行命令"></a>在远程服务上执行命令</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果需要在远程 redis 服务上执行命令，同样我们使用的也是 <strong>redis-cli</strong> 命令。</p>
<h3 id="语法-1"><a href="#语法-1" class="headerlink" title="语法"></a>语法</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis-cli -h host -p port -a password</div></pre></td></tr></table></figure>
<h3 id="实例-1"><a href="#实例-1" class="headerlink" title="实例"></a>实例</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下实例演示了如何连接到主机为 127.0.0.1，端口为 6379 ，密码为 mypass 的 redis 服务上。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">redis-cli -h 127.0.0.1 -p 6379 -a <span class="string">"mypass"</span></div><div class="line">redis 127.0.0.1:6379&gt;</div><div class="line">redis 127.0.0.1:6379&gt; PING</div><div class="line"> </div><div class="line">PONG</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Redis/16. 超强、超详细Redis数据库入门教程/">超强、超详细Redis数据库入门教程</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<h2 id="redis是什么"><a href="#redis是什么" class="headerlink" title="redis是什么"></a>redis是什么</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis是一个开源的、使用C语言编写的、支持网络交互的、可基于内存也可持久化的Key-Value数据库。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;目前，Vmware在资助着redis项目的开发和维护。</p>
<h2 id="学会安装redis"><a href="#学会安装redis" class="headerlink" title="学会安装redis"></a>学会安装redis</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从redis.io下载最新版redis-X.Y.Z.tar.gz后解压，然后进入redis-X.Y.Z文件夹后直接make即可，安装非常简单。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;make成功后会在src文件夹下产生一些二进制可执行文件，包括redis-server、redis-cli等等：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$ find . -<span class="built_in">type</span> f -executable</div><div class="line">./redis-benchmark     <span class="comment"># 用于进行redis性能测试的工具</span></div><div class="line">./redis-check-dump    <span class="comment"># 用于修复出问题的dump.rdb文件</span></div><div class="line">./redis-cli           <span class="comment"># redis的客户端</span></div><div class="line">./redis-server        <span class="comment"># redis的服务端</span></div><div class="line">./redis-check-aof     <span class="comment"># 用于修复出问题的AOF文件</span></div><div class="line">./redis-sentinel      <span class="comment"># 用于集群管理</span></div></pre></td></tr></table></figure>
<h2 id="学会启动redis"><a href="#学会启动redis" class="headerlink" title="学会启动redis"></a>学会启动redis</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动redis非常简单，直接./redis-server就可以启动服务端了，还可以用下面的方法指定要加载的配置文件：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">./redis-server ../redis.conf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认情况下，redis-server会以非daemon的方式来运行，且默认服务端口为6379。</p>
<h2 id="使用redis客户端"><a href="#使用redis客户端" class="headerlink" title="使用redis客户端"></a>使用redis客户端</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们直接看一个例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#这样来启动redis客户端了</span></div><div class="line">$ ./redis-cli</div><div class="line"><span class="comment">#用set指令来设置key、value</span></div><div class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> name <span class="string">"roc"</span> </div><div class="line">OK</div><div class="line"><span class="comment">#来获取name的值</span></div><div class="line">127.0.0.1:6379&gt; get name </div><div class="line"><span class="string">"roc"</span></div><div class="line"><span class="comment">#通过客户端来关闭redis服务端</span></div><div class="line">127.0.0.1:6379&gt; shutdown </div><div class="line">127.0.0.1:6379&gt;</div></pre></td></tr></table></figure>
<h2 id="redis数据结构-–-简介"><a href="#redis数据结构-–-简介" class="headerlink" title="redis数据结构 – 简介"></a>redis数据结构 – 简介</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis是一种高级的key:value存储系统，其中value支持五种数据类型：</p>
<ol>
<li>字符串（strings）</li>
<li>字符串列表（lists）</li>
<li>字符串集合（sets）</li>
<li>有序字符串集合（sorted sets）</li>
<li>哈希（hashes）</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;而关于key，有几个点要提醒大家：</p>
<ol>
<li>key不要太长，尽量不要超过1024字节，这不仅消耗内存，而且会降低查找的效率；</li>
<li>key也不要太短，太短的话，key的可读性会降低；</li>
<li>在一个项目中，key最好使用统一的命名模式，例如user:10000:passwd。</li>
</ol>
<h3 id="redis数据结构-–-strings"><a href="#redis数据结构-–-strings" class="headerlink" title="redis数据结构 – strings"></a>redis数据结构 – strings</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有人说，如果只使用redis中的字符串类型，且不使用redis的持久化功能，那么，redis就和memcache非常非常的像了。这说明strings类型是一个很基础的数据类型，也是任何存储系统都必备的数据类型。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们来看一个最简单的例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span> mystr <span class="string">"hello world!"</span>   <span class="comment"># 设置字符串类型</span></div><div class="line">get mystr                  <span class="comment"># 读取字符串类型</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;字符串类型的用法就是这么简单，因为是二进制安全的，所以你完全可以把一个图片文件的内容作为字符串来存储。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外，我们还可以通过字符串类型进行数值操作：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> mynum <span class="string">"2"</span></div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; get mynum</div><div class="line"><span class="string">"2"</span></div><div class="line">127.0.0.1:6379&gt; incr mynum</div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line">127.0.0.1:6379&gt; get mynum</div><div class="line"><span class="string">"3"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看，在遇到数值操作时，redis会将字符串类型转换成数值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;由于INCR等指令本身就具有原子操作的特性，所以我们完全可以利用redis的INCR、INCRBY、DECR、DECRBY等指令来实现原子计数的效果，假如，在某种场景下有3个客户端同时读取了mynum的值（值为2），然后对其同时进行了加1的操作，那么，最后mynum的值一定是5。不少网站都利用redis的这个特性来实现业务上的统计计数需求。</p>
<h3 id="redis数据结构-–-lists"><a href="#redis数据结构-–-lists" class="headerlink" title="redis数据结构 – lists"></a>redis数据结构 – lists</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis的另一个重要的数据结构叫做lists，翻译成中文叫做“列表”。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先要明确一点，redis中的lists在底层实现上并不是数组，而是链表，也就是说对于一个具有上百万个元素的lists来说，在头部和尾部插入一个新元素，其时间复杂度是常数级别的，比如用LPUSH在10个元素的lists头部插入新元素，和在上千万元素的lists头部插入新元素的速度应该是相同的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;虽然lists有这样的优势，但同样有其弊端，那就是，链表型lists的元素定位会比较慢，而数组型lists的元素定位就会快得多。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;lists的常用操作包括LPUSH、RPUSH、LRANGE等。我们可以用LPUSH在lists的左侧插入一个新元素，用RPUSH在lists的右侧插入一个新元素，用LRANGE命令从lists中指定一个范围来提取元素。我们来看几个例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#新建一个list叫做mylist，并在列表头部插入元素"1"</span></div><div class="line">127.0.0.1:6379&gt; lpush mylist <span class="string">"1"</span> </div><div class="line"><span class="comment">#返回当前mylist中的元素个数</span></div><div class="line">(<span class="built_in">integer</span>) 1 </div><div class="line"><span class="comment">#在mylist右侧插入元素"2"</span></div><div class="line">127.0.0.1:6379&gt; rpush mylist <span class="string">"2"</span> </div><div class="line">(<span class="built_in">integer</span>) 2</div><div class="line"><span class="comment">#在mylist左侧插入元素"0"</span></div><div class="line">127.0.0.1:6379&gt; lpush mylist <span class="string">"0"</span> </div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line"><span class="comment">#列出mylist中从编号0到编号1的元素</span></div><div class="line">127.0.0.1:6379&gt; lrange mylist 0 1 </div><div class="line">1) <span class="string">"0"</span></div><div class="line">2) <span class="string">"1"</span></div><div class="line"><span class="comment">#列出mylist中从编号0到倒数第一个元素</span></div><div class="line">127.0.0.1:6379&gt; lrange mylist 0 -1 </div><div class="line">1) <span class="string">"0"</span></div><div class="line">2) <span class="string">"1"</span></div><div class="line">3) <span class="string">"2"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;lists的应用相当广泛，随便举几个例子：</p>
<ol>
<li>我们可以利用lists来实现一个消息队列，而且可以确保先后顺序，不必像MySQL那样还需要通过ORDER BY来进行排序。</li>
<li>利用LRANGE还可以很方便的实现分页的功能。</li>
<li>在博客系统中，每片博文的评论也可以存入一个单独的list中。</li>
</ol>
<h3 id="redis数据结构-–-集合"><a href="#redis数据结构-–-集合" class="headerlink" title="redis数据结构 – 集合"></a>redis数据结构 – 集合</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis的集合，是一种无序的集合，集合中的元素没有先后顺序。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;集合相关的操作也很丰富，如添加新元素、删除已有元素、取交集、取并集、取差集等。我们来看例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#向集合myset中加入一个新元素"one"</span></div><div class="line">127.0.0.1:6379&gt; sadd myset <span class="string">"one"</span> </div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">127.0.0.1:6379&gt; sadd myset <span class="string">"two"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"><span class="comment">#列出集合myset中的所有元素</span></div><div class="line">127.0.0.1:6379&gt; smembers myset </div><div class="line">1) <span class="string">"one"</span></div><div class="line">2) <span class="string">"two"</span></div><div class="line"><span class="comment">#判断元素1是否在集合myset中，返回1表示存在</span></div><div class="line">127.0.0.1:6379&gt; sismember myset <span class="string">"one"</span> </div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"><span class="comment">#判断元素3是否在集合myset中，返回0表示不存在</span></div><div class="line">127.0.0.1:6379&gt; sismember myset <span class="string">"three"</span> </div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"><span class="comment">#新建一个新的集合yourset</span></div><div class="line">127.0.0.1:6379&gt; sadd yourset <span class="string">"1"</span> </div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">127.0.0.1:6379&gt; sadd yourset <span class="string">"2"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">127.0.0.1:6379&gt; smembers yourset</div><div class="line">1) <span class="string">"1"</span></div><div class="line">2) <span class="string">"2"</span></div><div class="line"><span class="comment">#对两个集合求并集</span></div><div class="line">127.0.0.1:6379&gt; sunion myset yourset </div><div class="line">1) <span class="string">"1"</span></div><div class="line">2) <span class="string">"one"</span></div><div class="line">3) <span class="string">"2"</span></div><div class="line">4) <span class="string">"two"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于集合的使用，也有一些常见的方式，比如，QQ有一个社交功能叫做“好友标签”，大家可以给你的好友贴标签，比如“大美女”、“土豪”、“欧巴”等等，这时就可以使用redis的集合来实现，把每一个用户的标签都存储在一个集合之中。</p>
<h3 id="redis数据结构-–-有序集合"><a href="#redis数据结构-–-有序集合" class="headerlink" title="redis数据结构 – 有序集合"></a>redis数据结构 – 有序集合</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis不但提供了无需集合（sets），还很体贴的提供了有序集合（sorted sets）。有序集合中的每个元素都关联一个序号（score），这便是排序的依据。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;很多时候，我们都将redis中的有序集合叫做zsets，这是因为在redis中，有序集合相关的操作指令都是以z开头的，比如zrange、zadd、zrevrange、zrangebyscore等等</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;老规矩，我们来看几个生动的例子：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;新增一个有序集合myzset，并加入一个元素baidu.com，给它赋予的序号是1：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; zadd myzset 1 baidu.com </div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"><span class="comment">#向myzset中新增一个元素360.com，赋予它的序号是3</span></div><div class="line">127.0.0.1:6379&gt; zadd myzset 3 360.com </div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"><span class="comment">#向myzset中新增一个元素google.com，赋予它的序号是2</span></div><div class="line">127.0.0.1:6379&gt; zadd myzset 2 google.com </div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"><span class="comment">#列出myzset的所有元素，同时列出其序号，可以看出myzset已经是有序的了。</span></div><div class="line">127.0.0.1:6379&gt; zrange myzset 0 -1 with scores </div><div class="line">1) <span class="string">"baidu.com"</span></div><div class="line">2) <span class="string">"1"</span></div><div class="line">3) <span class="string">"google.com"</span></div><div class="line">4) <span class="string">"2"</span></div><div class="line">5) <span class="string">"360.com"</span></div><div class="line">6) <span class="string">"3"</span></div><div class="line"><span class="comment">#只列出myzset的元素</span></div><div class="line">127.0.0.1:6379&gt; zrange myzset 0 -1 </div><div class="line">1) <span class="string">"baidu.com"</span></div><div class="line">2) <span class="string">"google.com"</span></div><div class="line">3) <span class="string">"360.com"</span></div></pre></td></tr></table></figure>
<h3 id="redis数据结构-–-哈希"><a href="#redis数据结构-–-哈希" class="headerlink" title="redis数据结构 – 哈希"></a>redis数据结构 – 哈希</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后要给大家介绍的是hashes，即哈希。哈希是从redis-2.0.0版本之后才有的数据结构。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;hashes存的是字符串和字符串值之间的映射，比如一个用户要存储其全名、姓氏、年龄等等，就很适合使用哈希。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们来看一个例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#建立哈希，并赋值</span></div><div class="line">127.0.0.1:6379&gt; HMSET user:001 username antirez password P1pp0 age 34 </div><div class="line">OK</div><div class="line"><span class="comment">#列出哈希的内容</span></div><div class="line">127.0.0.1:6379&gt; HGETALL user:001 </div><div class="line">1) <span class="string">"username"</span></div><div class="line">2) <span class="string">"antirez"</span></div><div class="line">3) <span class="string">"password"</span></div><div class="line">4) <span class="string">"P1pp0"</span></div><div class="line">5) <span class="string">"age"</span></div><div class="line">6) <span class="string">"34"</span></div><div class="line"><span class="comment">#更改哈希中的某一个值</span></div><div class="line">127.0.0.1:6379&gt; HSET user:001 password 12345 </div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"><span class="comment">#再次列出哈希的内容</span></div><div class="line">127.0.0.1:6379&gt; HGETALL user:001 </div><div class="line">1) <span class="string">"username"</span></div><div class="line">2) <span class="string">"antirez"</span></div><div class="line">3) <span class="string">"password"</span></div><div class="line">4) <span class="string">"12345"</span></div><div class="line">5) <span class="string">"age"</span></div><div class="line">6) <span class="string">"34"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有关hashes的操作，同样很丰富，需要时，大家可以从这里查询。</p>
<h2 id="redis持久化-–-两种方式"><a href="#redis持久化-–-两种方式" class="headerlink" title="redis持久化 – 两种方式"></a>redis持久化 – 两种方式</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis提供了两种持久化的方式，分别是RDB（Redis DataBase）和AOF（Append Only File）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;RDB，简而言之，就是在不同的时间点，将redis存储的数据生成快照并存储到磁盘等介质上；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;AOF，则是换了一个角度来实现持久化，那就是将redis执行过的所有写指令记录下来，在下次redis重新启动时，只要把这些写指令从前到后再重复执行一遍，就可以实现数据恢复了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其实RDB和AOF两种方式也可以同时使用，在这种情况下，如果redis重启的话，则会优先采用AOF方式来进行数据恢复，这是因为AOF方式的数据恢复完整度更高。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果你没有数据持久化的需求，也完全可以关闭RDB和AOF方式，这样的话，redis将变成一个纯内存数据库，就像memcache一样。</p>
<h2 id="redis持久化-–-RDB"><a href="#redis持久化-–-RDB" class="headerlink" title="redis持久化 – RDB"></a>redis持久化 – RDB</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;RDB方式，是将redis某一时刻的数据持久化到磁盘中，是一种快照式的持久化方法。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis在进行数据持久化的过程中，会先将数据写入到一个临时文件中，待持久化过程都结束了，才会用这个临时文件替换上次持久化好的文件。正是这种特性，让我们可以随时来进行备份，因为快照文件总是完整可用的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于RDB方式，redis会单独创建（fork）一个子进程来进行持久化，而主进程是不会进行任何IO操作的，这样就确保了redis极高的性能。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果需要进行大规模数据的恢复，且对于数据恢复的完整性不是非常敏感，那RDB方式要比AOF方式更加的高效。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;虽然RDB有不少优点，但它的缺点也是不容忽视的。如果你对数据的完整性非常敏感，那么RDB方式就不太适合你，因为即使你每5分钟都持久化一次，当redis故障时，仍然会有近5分钟的数据丢失。所以，redis还提供了另一种持久化方式，那就是AOF。</p>
<h2 id="redis持久化-–-AOF"><a href="#redis持久化-–-AOF" class="headerlink" title="redis持久化 – AOF"></a>redis持久化 – AOF</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;AOF，英文是Append Only File，即只允许追加不允许改写的文件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如前面介绍的，AOF方式是将执行过的写指令记录下来，在数据恢复时按照从前到后的顺序再将指令都执行一遍，就这么简单。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们通过配置redis.conf中的appendonly yes就可以打开AOF功能。如果有写操作（如SET等），redis就会被追加到AOF文件的末尾。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认的AOF持久化策略是每秒钟fsync一次（fsync是指把缓存中的写指令记录到磁盘中），因为在这种情况下，redis仍然可以保持很好的处理性能，即使redis故障，也只会丢失最近1秒钟的数据。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果在追加日志时，恰好遇到磁盘空间满、inode满或断电等情况导致日志写入不完整，也没有关系，redis提供了redis-check-aof工具，可以用来进行日志修复。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为采用了追加方式，如果不做任何处理的话，AOF文件会变得越来越大，为此，redis提供了AOF文件重写（rewrite）机制，即当AOF文件的大小超过所设定的阈值时，redis就会启动AOF文件的内容压缩，只保留可以恢复数据的最小指令集。举个例子或许更形象，假如我们调用了100次INCR指令，在AOF文件中就要存储100条指令，但这明显是很低效的，完全可以把这100条指令合并成一条SET指令，这就是重写机制的原理。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在进行AOF重写时，仍然是采用先写临时文件，全部完成后再替换的流程，所以断电、磁盘满等问题都不会影响AOF文件的可用性，这点大家可以放心。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;AOF方式的另一个好处，我们通过一个“场景再现”来说明。某同学在操作redis时，不小心执行了FLUSHALL，导致redis内存中的数据全部被清空了，这是很悲剧的事情。不过这也不是世界末日，只要redis配置了AOF持久化方式，且AOF文件还没有被重写（rewrite），我们就可以用最快的速度暂停redis并编辑AOF文件，将最后一行的FLUSHALL命令删除，然后重启redis，就可以恢复redis的所有数据到FLUSHALL之前的状态了。是不是很神奇，这就是AOF持久化方式的好处之一。但是如果AOF文件已经被重写了，那就无法通过这种方法来恢复数据了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;虽然优点多多，但AOF方式也同样存在缺陷，比如在同样数据规模的情况下，AOF文件要比RDB文件的体积大。而且，AOF方式的恢复速度也要慢于RDB方式。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果你直接执行BGREWRITEAOF命令，那么redis会生成一个全新的AOF文件，其中便包括了可以恢复现有数据的最少的命令集。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果运气比较差，AOF文件出现了被写坏的情况，也不必过分担忧，redis并不会贸然加载这个有问题的AOF文件，而是报错退出。这时可以通过以下步骤来修复出错的文件：</p>
<ol>
<li>备份被写坏的AOF文件</li>
<li>运行redis-check-aof –fix进行修复</li>
<li>用diff -u来看下两个文件的差异，确认问题点</li>
<li>重启redis，加载修复后的AOF文件</li>
</ol>
<h2 id="redis持久化-–-AOF重写"><a href="#redis持久化-–-AOF重写" class="headerlink" title="redis持久化 – AOF重写"></a>redis持久化 – AOF重写</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;AOF重写的内部运行原理，我们有必要了解一下。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在重写即将开始之际，redis会创建（fork）一个“重写子进程”，这个子进程会首先读取现有的AOF文件，并将其包含的指令进行分析压缩并写入到一个临时文件中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;与此同时，主工作进程会将新接收到的写指令一边累积到内存缓冲区中，一边继续写入到原有的AOF文件中，这样做是保证原有的AOF文件的可用性，避免在重写过程中出现意外。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当“重写子进程”完成重写工作后，它会给父进程发一个信号，父进程收到信号后就会将内存中缓存的写指令追加到新AOF文件中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当追加结束后，redis就会用新AOF文件来代替旧AOF文件，之后再有新的写指令，就都会追加到新的AOF文件中了。</p>
<h2 id="redis持久化-–-如何选择RDB和AOF"><a href="#redis持久化-–-如何选择RDB和AOF" class="headerlink" title="redis持久化 – 如何选择RDB和AOF"></a>redis持久化 – 如何选择RDB和AOF</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于我们应该选择RDB还是AOF，官方的建议是两个同时使用。这样可以提供更可靠的持久化方案。</p>
<h2 id="主从-–-用法"><a href="#主从-–-用法" class="headerlink" title="主从 – 用法"></a>主从 – 用法</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;像MySQL一样，redis是支持主从同步的，而且也支持一主多从以及多级从结构。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主从结构，一是为了纯粹的冗余备份，二是为了提升读性能，比如很消耗性能的SORT就可以由从服务器来承担。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis的主从同步是异步进行的，这意味着主从同步不会影响主逻辑，也不会降低redis的处理性能。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主从架构中，可以考虑关闭主服务器的数据持久化功能，只让从服务器进行持久化，这样可以提高主服务器的处理性能。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在主从架构中，从服务器通常被设置为只读模式，这样可以避免从服务器的数据被误修改。但是从服务器仍然可以接受CONFIG等指令，所以还是不应该将从服务器直接暴露到不安全的网络环境中。如果必须如此，那可以考虑给重要指令进行重命名，来避免命令被外人误执行。</p>
<h2 id="主从-–-同步原理"><a href="#主从-–-同步原理" class="headerlink" title="主从 – 同步原理"></a>主从 – 同步原理</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从服务器会向主服务器发出SYNC指令，当主服务器接到此命令后，就会调用BGSAVE指令来创建一个子进程专门进行数据持久化工作，也就是将主服务器的数据写入RDB文件中。在数据持久化期间，主服务器将执行的写指令都缓存在内存中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在BGSAVE指令执行完成后，主服务器会将持久化好的RDB文件发送给从服务器，从服务器接到此文件后会将其存储到磁盘上，然后再将其读取到内存中。这个动作完成后，主服务器会将这段时间缓存的写指令再以redis协议的格式发送给从服务器。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外，要说的一点是，即使有多个从服务器同时发来SYNC指令，主服务器也只会执行一次BGSAVE，然后把持久化好的RDB文件发给多个下游。在redis2.8版本之前，如果从服务器与主服务器因某些原因断开连接的话，都会进行一次主从之间的全量的数据同步；而在2.8版本之后，redis支持了效率更高的增量同步策略，这大大降低了连接断开的恢复成本。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主服务器会在内存中维护一个缓冲区，缓冲区中存储着将要发给从服务器的内容。从服务器在与主服务器出现网络瞬断之后，从服务器会尝试再次与主服务器连接，一旦连接成功，从服务器就会把“希望同步的主服务器ID”和“希望请求的数据的偏移位置（replication offset）”发送出去。主服务器接收到这样的同步请求后，首先会验证主服务器ID是否和自己的ID匹配，其次会检查“请求的偏移位置”是否存在于自己的缓冲区中，如果两者都满足的话，主服务器就会向从服务器发送增量内容。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增量同步功能，需要服务器端支持全新的PSYNC指令。这个指令，只有在redis-2.8之后才具有。</p>
<h2 id="redis的事务处理"><a href="#redis的事务处理" class="headerlink" title="redis的事务处理"></a>redis的事务处理</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;众所周知，事务是指“一个完整的动作，要么全部执行，要么什么也没有做”。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在聊redis事务处理之前，要先和大家介绍四个redis指令，即MULTI、EXEC、DISCARD、WATCH。这四个指令构成了redis事务处理的基础。</p>
<ol>
<li>MULTI用来组装一个事务；</li>
<li>EXEC用来执行一个事务；</li>
<li>DISCARD用来取消一个事务；</li>
<li>WATCH用来监视一些key，一旦这些key在事务执行之前被改变，则取消事务的执行。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;纸上得来终觉浅，我们来看一个MULTI和EXEC的例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">redis&gt; MULTI //标记事务开始</div><div class="line">OK</div><div class="line">redis&gt; INCR user_id //多条命令按顺序入队</div><div class="line">QUEUED</div><div class="line">redis&gt; INCR user_id</div><div class="line">QUEUED</div><div class="line">redis&gt; INCR user_id</div><div class="line">QUEUED</div><div class="line">redis&gt; PING</div><div class="line">QUEUED</div><div class="line">redis&gt; EXEC //执行</div><div class="line">1) (<span class="built_in">integer</span>) 1</div><div class="line">2) (<span class="built_in">integer</span>) 2</div><div class="line">3) (<span class="built_in">integer</span>) 3</div><div class="line">4) PONG</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在上面的例子中，我们看到了QUEUED的字样，这表示我们在用MULTI组装事务时，每一个命令都会进入到内存队列中缓存起来，如果出现QUEUED则表示我们这个命令成功插入了缓存队列，在将来执行EXEC时，这些被QUEUED的命令都会被组装成一个事务来执行。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于事务的执行来说，如果redis开启了AOF持久化的话，那么一旦事务被成功执行，事务中的命令就会通过write命令一次性写到磁盘中去，如果在向磁盘中写的过程中恰好出现断电、硬件故障等问题，那么就可能出现只有部分命令进行了AOF持久化，这时AOF文件就会出现不完整的情况，这时，我们可以使用redis-check-aof工具来修复这一问题，这个工具会将AOF文件中不完整的信息移除，确保AOF文件完整可用。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有关事务，大家经常会遇到的是两类错误：</p>
<ol>
<li>调用EXEC之前的错误</li>
<li>调用EXEC之后的错误</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;“调用EXEC之前的错误”，有可能是由于语法有误导致的，也可能时由于内存不足导致的。只要出现某个命令无法成功写入缓冲队列的情况，redis都会进行记录，在客户端调用EXEC时，redis会拒绝执行这一事务。（这时2.6.5版本之后的策略。在2.6.5之前的版本中，redis会忽略那些入队失败的命令，只执行那些入队成功的命令）。我们来看一个这样的例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; multi</div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; haha //一个明显错误的指令</div><div class="line">(error) ERR unknown <span class="built_in">command</span> <span class="string">'haha'</span></div><div class="line">127.0.0.1:6379&gt; ping</div><div class="line">QUEUED</div><div class="line">127.0.0.1:6379&gt; <span class="built_in">exec</span></div><div class="line">//redis无情的拒绝了事务的执行，原因是“之前出现了错误”</div><div class="line">(error) EXECABORT Transaction discarded because of previous errors.</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;而对于“调用EXEC之后的错误”，redis则采取了完全不同的策略，即redis不会理睬这些错误，而是继续向下执行事务中的其他命令。这是因为，对于应用层面的错误，并不是redis自身需要考虑和处理的问题，所以一个事务中如果某一条命令执行失败，并不会影响接下来的其他命令的执行。我们也来看一个例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; multi</div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> age 23</div><div class="line">QUEUED</div><div class="line"><span class="comment">#age不是集合，所以如下是一条明显错误的指令</span></div><div class="line">127.0.0.1:6379&gt; sadd age 15 </div><div class="line">QUEUED</div><div class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> age 29</div><div class="line">QUEUED</div><div class="line">127.0.0.1:6379&gt; <span class="built_in">exec</span>        <span class="comment"># 执行事务时，redis不会理睬第2条指令执行错误</span></div><div class="line">1) OK</div><div class="line">2) (error) WRONGTYPE Operation against a key holding the wrong kind of value</div><div class="line">3) OK</div><div class="line">127.0.0.1:6379&gt; get age</div><div class="line"><span class="string">"29"</span>                        <span class="comment"># 可以看出第3条指令被成功执行了</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;好了，我们来说说最后一个指令“WATCH”，这是一个很好用的指令，它可以帮我们实现类似于“乐观锁”的效果，即CAS（check and set）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;WATCH本身的作用是“监视key是否被改动过”，而且支持同时监视多个key，只要还没真正触发事务，WATCH都会尽职尽责的监视，一旦发现某个key被修改了，在执行EXEC时就会返回nil，表示事务无法触发。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> age 23</div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; watch age   <span class="comment"># 开始监视age</span></div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> age 24  <span class="comment"># 在EXEC之前，age的值被修改了</span></div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; multi</div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> age 25</div><div class="line">QUEUED</div><div class="line">127.0.0.1:6379&gt; get age</div><div class="line">QUEUED</div><div class="line">127.0.0.1:6379&gt; <span class="built_in">exec</span> //触发EXEC</div><div class="line">(nil)                       <span class="comment"># 事务无法被执行</span></div></pre></td></tr></table></figure>
<h2 id="redis配置-–-简介"><a href="#redis配置-–-简介" class="headerlink" title="redis配置 – 简介"></a>redis配置 – 简介</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们可以在启动redis-server时指定应该加载的配置文件，方法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ ./redis-server /path/to/redis.conf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;接下来，我们就来讲解下redis配置文件的各个配置项的含义，注意，本文是基于redis-2.8.4版本进行讲解的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis官方提供的redis.conf文件，足有700+行，其中100多行为有效配置行，另外的600多行为注释说明。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在配置文件的开头部分，首先明确了一些度量单位：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 1k =&gt; 1000 bytes</span></div><div class="line"><span class="comment"># 1kb =&gt; 1024 bytes</span></div><div class="line"><span class="comment"># 1m =&gt; 1000000 bytes</span></div><div class="line"><span class="comment"># 1mb =&gt; 1024*1024 bytes</span></div><div class="line"><span class="comment"># 1g =&gt; 1000000000 bytes</span></div><div class="line"><span class="comment"># 1gb =&gt; 1024*1024*1024 bytes</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以看出，redis配置中对单位的大小写不敏感，1GB、1Gb和1gB都是相同的。由此也说明，redis只支持bytes，不支持bit单位。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis支持“主配置文件中引入外部配置文件”，很像C/C++中的include指令，比如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">include /path/to/other.conf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果你看过redis的配置文件，会发现还是很有条理的。redis配置文件被分成了几大块区域，它们分别是：</p>
<ol>
<li>通用（general）</li>
<li>快照（snapshotting）</li>
<li>复制（replication）</li>
<li>安全（security）</li>
<li>限制（limits)</li>
<li>追加模式（append only mode)</li>
<li>LUA脚本（lua scripting)</li>
<li>慢日志（slow log)</li>
<li>事件通知（event notification）</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面我们就来逐一讲解。</p>
<h3 id="redis配置-通用"><a href="#redis配置-通用" class="headerlink" title="redis配置 -通用"></a>redis配置 -通用</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认情况下，redis并不是以daemon形式来运行的。通过daemonize配置项可以控制redis的运行形式，如果改为yes，那么redis就会以daemon形式运行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">daemonize no</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当以daemon形式运行时，redis会生成一个pid文件，默认会生成在/var/run/redis.pid。当然，你可以通过pidfile来指定pid文件生成的位置，比如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pidfile /path/to/redis.pid</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认情况下，redis会响应本机所有可用网卡的连接请求。当然，redis允许你通过bind配置项来指定要绑定的IP，比如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">bind</span> 192.168.1.2 10.8.4.2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis的默认服务端口是6379，你可以通过port配置项来修改。如果端口设置为0的话，redis便不会监听端口了。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">port 6379</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有些同学会问“如果redis不监听端口，还怎么与外界通信呢”，其实redis还支持通过unix socket方式来接收请求。可以通过unixsocket配置项来指定unix socket文件的路径，并通过unixsocketperm来指定文件的权限。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">unixsocket /tmp/redis.sock</div><div class="line">unixsocketperm 755</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当一个redis-client一直没有请求发向server端，那么server端有权主动关闭这个连接，可以通过timeout来设置“空闲超时时限”，0表示永不关闭。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">timeout 0</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;TCP连接保活策略，可以通过tcp-keepalive配置项来进行设置，单位为秒，假如设置为60秒，则server端会每60秒向连接空闲的客户端发起一次ACK请求，以检查客户端是否已经挂掉，对于无响应的客户端则会关闭其连接。所以关闭一个连接最长需要120秒的时间。如果设置为0，则不会进行保活检测。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tcp-keepalive 0</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis支持通过loglevel配置项设置日志等级，共分四级，即debug、verbose、notice、warning。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">loglevel notice</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis也支持通过logfile配置项来设置日志文件的生成位置。如果设置为空字符串，则redis会将日志输出到标准输出。假如你在daemon情况下将日志设置为输出到标准输出，则日志会被写到/dev/null中。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">logfile <span class="string">""</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果希望日志打印到syslog中，也很容易，通过syslog-enabled来控制。另外，syslog-ident还可以让你指定syslog里的日志标志，比如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">syslog-ident redis</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;而且还支持指定syslog设备，值可以是USER或LOCAL0-LOCAL7。具体可以参考syslog服务本身的用法。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">syslog-facility local0</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于redis来说，可以设置其数据库的总数量，假如你希望一个redis包含16个数据库，那么设置如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">databases 16</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这16个数据库的编号将是0到15。默认的数据库是编号为0的数据库。用户可以使用select <dbid>来选择相应的数据库。</dbid></p>
<h3 id="redis配置-–-快照"><a href="#redis配置-–-快照" class="headerlink" title="redis配置 – 快照"></a>redis配置 – 快照</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;快照，主要涉及的是redis的RDB持久化相关的配置，我们来一起看一看。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们可以用如下的指令来让数据保存到磁盘上，即控制RDB快照功能：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">save &lt;seconds&gt; &lt;changes&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;举例来说：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">save 900 1     <span class="comment"># 表示每15分钟且至少有1个key改变，就触发一次持久化</span></div><div class="line">save 300 10    <span class="comment"># 表示每5分钟且至少有10个key改变，就触发一次持久化</span></div><div class="line">save 60 10000  <span class="comment"># 表示每60秒至少有10000个key改变，就触发一次持久化</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果你想禁用RDB持久化的策略，只要不设置任何save指令就可以，或者给save传入一个空字符串参数也可以达到相同效果，就像这样：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">save <span class="string">""</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果用户开启了RDB快照功能，那么在redis持久化数据到磁盘时如果出现失败，默认情况下，redis会停止接受所有的写请求。这样做的好处在于可以让用户很明确的知道内存中的数据和磁盘上的数据已经存在不一致了。如果redis不顾这种不一致，一意孤行的继续接收写请求，就可能会引起一些灾难性的后果。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果下一次RDB持久化成功，redis会自动恢复接受写请求。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当然，如果你不在乎这种数据不一致或者有其他的手段发现和控制这种不一致的话，你完全可以关闭这个功能，以便在快照写入失败时，也能确保redis继续接受新的写请求。配置项如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">stop-writes-on-bgsave-error yes</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于存储到磁盘中的快照，可以设置是否进行压缩存储。如果是的话，redis会采用LZF算法进行压缩。如果你不想消耗CPU来进行压缩的话，可以设置为关闭此功能，但是存储在磁盘上的快照会比较大。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rdbcompression yes</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在存储快照后，我们还可以让redis使用CRC64算法来进行数据校验，但是这样做会增加大约10%的性能消耗，如果你希望获取到最大的性能提升，可以关闭此功能。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rdbchecksum yes</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们还可以设置快照文件的名称，默认是这样配置的：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">dbfilename dump.rdb</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后，你还可以设置这个快照文件存放的路径。比如默认设置就是当前文件夹：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">dir ./</div></pre></td></tr></table></figure>
<h3 id="redis配置-–-复制"><a href="#redis配置-–-复制" class="headerlink" title="redis配置 – 复制"></a>redis配置 – 复制</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis提供了主从同步功能。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;通过slaveof配置项可以控制某一个redis作为另一个redis的从服务器，通过指定IP和端口来定位到主redis的位置。一般情况下，我们会建议用户为从redis设置一个不同频率的快照持久化的周期，或者为从redis配置一个不同的服务端口等等。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">slaveof &lt;masterip&gt; &lt;masterport&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果主redis设置了验证密码的话（使用requirepass来设置），则在从redis的配置中要使用masterauth来设置校验密码，否则的话，主redis会拒绝从redis的访问请求。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">masterauth &lt;master-password&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当从redis失去了与主redis的连接，或者主从同步正在进行中时，redis该如何处理外部发来的访问请求呢？这里，从redis可以有两种选择：</p>
<ul>
<li>第一种选择：如果slave-serve-stale-data设置为yes（默认），则从redis仍会继续响应客户端的读写请求。</li>
<li>第二种选择：如果slave-serve-stale-data设置为no，则从redis会对客户端的请求返回“SYNC with master in progress”，当然也有例外，当客户端发来INFO请求和SLAVEOF请求，从redis还是会进行处理。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;你可以控制一个从redis是否可以接受写请求。将数据直接写入从redis，一般只适用于那些生命周期非常短的数据，因为在主从同步时，这些临时数据就会被清理掉。自从redis2.6版本之后，默认从redis为只读。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">slave-read-only yes</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;只读的从redis并不适合直接暴露给不可信的客户端。为了尽量降低风险，可以使用rename-command指令来将一些可能有破坏力的命令重命名，避免外部直接调用。比如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从redis会周期性的向主redis发出PING包。你可以通过repl_ping_slave_period指令来控制其周期。默认是10秒。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">repl-ping-slave-period 10</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在主从同步时，可能在这些情况下会有超时发生：</p>
<ol>
<li>以从redis的角度来看，当有大规模IO传输时。</li>
<li>以从redis的角度来看，当数据传输或PING时，主redis超时</li>
<li>以主redis的角度来看，在回复从redis的PING时，从redis超时</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用户可以设置上述超时的时限，不过要确保这个时限比repl-ping-slave-period的值要大，否则每次主redis都会认为从redis超时。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">repl-timeout 60</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们可以控制在主从同步时是否禁用TCP_NODELAY。如果开启TCP_NODELAY，那么主redis会使用更少的TCP包和更少的带宽来向从redis传输数据。但是这可能会增加一些同步的延迟，大概会达到40毫秒左右。如果你关闭了TCP_NODELAY，那么数据同步的延迟时间会降低，但是会消耗更多的带宽。（如果你不了解TCP_NODELAY，可以到这里来科普一下）。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">repl-disable-tcp-nodelay no</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们还可以设置同步队列长度。队列长度（backlog)是主redis中的一个缓冲区，在与从redis断开连接期间，主redis会用这个缓冲区来缓存应该发给从redis的数据。这样的话，当从redis重新连接上之后，就不必重新全量同步数据，只需要同步这部分增量数据即可。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">repl-backlog-size 1mb</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果主redis等了一段时间之后，还是无法连接到从redis，那么缓冲队列中的数据将被清理掉。我们可以设置主redis要等待的时间长度。如果设置为0，则表示永远不清理。默认是1个小时。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">repl-backlog-ttl 3600</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们可以给众多的从redis设置优先级，在主redis持续工作不正常的情况，优先级高的从redis将会升级为主redis。而编号越小，优先级越高。比如一个主redis有三个从redis，优先级编号分别为10、100、25，那么编号为10的从redis将会被首先选中升级为主redis。当优先级被设置为0时，这个从redis将永远也不会被选中。默认的优先级为100。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">slave-priority 100</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;假如主redis发现有超过M个从redis的连接延时大于N秒，那么主redis就停止接受外来的写请求。这是因为从redis一般会每秒钟都向主redis发出PING，而主redis会记录每一个从redis最近一次发来PING的时间点，所以主redis能够了解每一个从redis的运行情况。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">min-slaves-to-write 3</div><div class="line">min-slaves-max-lag 10</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面这个例子表示，假如有大于等于3个从redis的连接延迟大于10秒，那么主redis就不再接受外部的写请求。上述两个配置中有一个被置为0，则这个特性将被关闭。默认情况下min-slaves-to-write为0，而min-slaves-max-lag为10。</p>
<h3 id="redis配置-–-安全"><a href="#redis配置-–-安全" class="headerlink" title="redis配置 – 安全"></a>redis配置 – 安全</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们可以要求redis客户端在向redis-server发送请求之前，先进行密码验证。当你的redis-server处于一个不太可信的网络环境中时，相信你会用上这个功能。由于redis性能非常高，所以每秒钟可以完成多达15万次的密码尝试，所以你最好设置一个足够复杂的密码，否则很容易被黑客破解。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">requirepass zhimakaimen</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里我们通过requirepass将密码设置成“芝麻开门”。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis允许我们对redis指令进行更名，比如将一些比较危险的命令改个名字，避免被误执行。比如可以把CONFIG命令改成一个很复杂的名字，这样可以避免外部的调用，同时还可以满足内部调用的需要：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c89</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们甚至可以禁用掉CONFIG命令，那就是把CONFIG的名字改成一个空字符串：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rename-command CONFIG <span class="string">""</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;但需要注意的是，如果你使用AOF方式进行数据持久化，或者需要与从redis进行通信，那么更改指令的名字可能会引起一些问题。</p>
<h3 id="redis配置-限制"><a href="#redis配置-限制" class="headerlink" title="redis配置 -限制"></a>redis配置 -限制</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们可以设置redis同时可以与多少个客户端进行连接。默认情况下为10000个客户端。当你无法设置进程文件句柄限制时，redis会设置为当前的文件句柄限制值减去32，因为redis会为自身内部处理逻辑留一些句柄出来。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果达到了此限制，redis则会拒绝新的连接请求，并且向这些连接请求方发出“max number of clients reached”以作回应。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">maxclients 10000</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们甚至可以设置redis可以使用的内存量。一旦到达内存使用上限，redis将会试图移除内部数据，移除规则可以通过maxmemory-policy来指定。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果redis无法根据移除规则来移除内存中的数据，或者我们设置了“不允许移除”，那么redis则会针对那些需要申请内存的指令返回错误信息，比如SET、LPUSH等。但是对于无内存申请的指令，仍然会正常响应，比如GET等。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">maxmemory &lt;bytes&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;需要注意的一点是，如果你的redis是主redis（说明你的redis有从redis），那么在设置内存使用上限时，需要在系统中留出一些内存空间给同步队列缓存，只有在你设置的是“不移除”的情况下，才不用考虑这个因素。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于内存移除规则来说，redis提供了多达6种的移除规则。他们是：</p>
<ol>
<li>volatile-lru：使用LRU算法移除过期集合中的key</li>
<li>allkeys-lru：使用LRU算法移除key</li>
<li>volatile-random：在过期集合中移除随机的key</li>
<li>allkeys-random：移除随机的key</li>
<li>volatile-ttl：移除那些TTL值最小的key，即那些最近才过期的key。</li>
<li>noeviction：不进行移除。针对写操作，只是返回错误信息。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;无论使用上述哪一种移除规则，如果没有合适的key可以移除的话，redis都会针对写请求返回错误信息。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">maxmemory-policy volatile-lru</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;LRU算法和最小TTL算法都并非是精确的算法，而是估算值。所以你可以设置样本的大小。假如redis默认会检查三个key并选择其中LRU的那个，那么你可以改变这个key样本的数量。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">maxmemory-samples 3</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后，我们补充一个信息，那就是到目前版本（2.8.4）为止，redis支持的写指令包括了如下这些：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span> setnx setex append</div><div class="line">incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd</div><div class="line">sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby</div><div class="line">zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby</div><div class="line">getset mset msetnx <span class="built_in">exec</span> sort</div></pre></td></tr></table></figure>
<h3 id="redis配置-–-追加模式"><a href="#redis配置-–-追加模式" class="headerlink" title="redis配置 – 追加模式"></a>redis配置 – 追加模式</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认情况下，redis会异步的将数据持久化到磁盘。这种模式在大部分应用程序中已被验证是很有效的，但是在一些问题发生时，比如断电，则这种机制可能会导致数分钟的写请求丢失。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如上半部分中介绍的，追加文件（Append Only File）是一种更好的保持数据一致性的方式。即使当服务器断电时，也仅会有1秒钟的写请求丢失，当redis进程出现问题且操作系统运行正常时，甚至只会丢失一条写请求。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们建议大家，AOF机制和RDB机制可以同时使用，不会有任何冲突。对于如何保持数据一致性的讨论，请参见本文。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">appendonly no</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们还可以设置aof文件的名称：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">appendfilename <span class="string">"appendonly.aof"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;fsync()调用，用来告诉操作系统立即将缓存的指令写入磁盘。一些操作系统会“立即”进行，而另外一些操作系统则会“尽快”进行。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis支持三种不同的模式：</p>
<ol>
<li>no：不调用fsync()。而是让操作系统自行决定sync的时间。这种模式下，redis的性能会最快。</li>
<li>always：在每次写请求后都调用fsync()。这种模式下，redis会相对较慢，但数据最安全。</li>
<li>everysec：每秒钟调用一次fsync()。这是性能和安全的折衷。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认情况下为everysec。有关数据一致性的揭秘，可以参考本文。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">appendfsync everysec</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当fsync方式设置为always或everysec时，如果后台持久化进程需要执行一个很大的磁盘IO操作，那么redis可能会在fsync()调用时卡住。目前尚未修复这个问题，这是因为即使我们在另一个新的线程中去执行fsync()，也会阻塞住同步写调用。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为了缓解这个问题，我们可以使用下面的配置项，这样的话，当BGSAVE或BGWRITEAOF运行时，fsync()在主进程中的调用会被阻止。这意味着当另一路进程正在对AOF文件进行重构时，redis的持久化功能就失效了，就好像我们设置了“appendsync none”一样。如果你的redis有时延问题，那么请将下面的选项设置为yes。否则请保持no，因为这是保证数据完整性的最安全的选择。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">no-appendfsync-on-rewrite no</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们允许redis自动重写aof。当aof增长到一定规模时，redis会隐式调用BGREWRITEAOF来重写log文件，以缩减文件体积。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis是这样工作的：redis会记录上次重写时的aof大小。假如redis自启动至今还没有进行过重写，那么启动时aof文件的大小会被作为基准值。这个基准值会和当前的aof大小进行比较。如果当前aof大小超出所设置的增长比例，则会触发重写。另外，你还需要设置一个最小大小，是为了防止在aof很小时就触发重写。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">auto-aof-rewrite-percentage 100</div><div class="line">auto-aof-rewrite-min-size 64mb</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果设置auto-aof-rewrite-percentage为0，则会关闭此重写功能。</p>
<h3 id="redis配置-–-LUA脚本"><a href="#redis配置-–-LUA脚本" class="headerlink" title="redis配置 – LUA脚本"></a>redis配置 – LUA脚本</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;lua脚本的最大运行时间是需要被严格限制的，要注意单位是毫秒：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">lua-time-limit 5000</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果此值设置为0或负数，则既不会有报错也不会有时间限制。</p>
<h3 id="redis配置-–-慢日志"><a href="#redis配置-–-慢日志" class="headerlink" title="redis配置 – 慢日志"></a>redis配置 – 慢日志</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis慢日志是指一个系统进行日志查询超过了指定的时长。这个时长不包括IO操作，比如与客户端的交互、发送响应内容等，而仅包括实际执行查询命令的时间。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;针对慢日志，你可以设置两个参数，一个是执行时长，单位是微秒，另一个是慢日志的长度。当一个新的命令被写入日志时，最老的一条会从命令日志队列中被移除。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;单位是微秒，即1000000表示一秒。负数则会禁用慢日志功能，而0则表示强制记录每一个命令。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">slowlog-log-slower-than 10000</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;慢日志最大长度，可以随便填写数值，没有上限，但要注意它会消耗内存。你可以使用SLOWLOG RESET来重设这个值。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">slowlog-max-len 128</div></pre></td></tr></table></figure>
<h3 id="redis配置-–-事件通知"><a href="#redis配置-–-事件通知" class="headerlink" title="redis配置 – 事件通知"></a>redis配置 – 事件通知</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis可以向客户端通知某些事件的发</p>
<h3 id="redis配置-–-高级配置"><a href="#redis配置-–-高级配置" class="headerlink" title="redis配置 – 高级配置"></a>redis配置 – 高级配置</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有关哈希数据结构的一些配置项：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">hash</span>-max-ziplist-entries 512</div><div class="line"><span class="built_in">hash</span>-max-ziplist-value 64</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有关列表数据结构的一些配置项：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">list-max-ziplist-entries 512</div><div class="line">list-max-ziplist-value 64</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有关集合数据结构的配置项：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span>-max-intset-entries 512</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有关有序集合数据结构的配置项：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">zset-max-ziplist-entries 128</div><div class="line">zset-max-ziplist-value 64</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关于是否需要再哈希的配置项：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">activerehashing yes</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关于客户端输出缓冲的控制项：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">client-output-buffer-limit normal 0 0 0</div><div class="line">client-output-buffer-limit slave 256mb 64mb 60</div><div class="line">client-output-buffer-limit pubsub 32mb 8mb 60</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有关频率的配置项：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">hz 10</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有关重写aof的配置项</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">aof-rewrite-incremental-fsync yes</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;至此，redis的入门内容就结束了.</p>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/18">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
